Skip to content
Advertisement

React-Redux: Data not read during initial render in useState

I am trying to get a list of data called packages and put it in a table. Added filter and sorting to it.

  • I get packages from pakageList reducer
  • Assign to data state
  • Then add soring and filtering logic and assign data to variable called filteredPackages Everything seems fine but when the page initially loads the contents of the table is empty i.e, the data state is empty. This happens while adding or deleting a package and render occurs as well. Once I go back and come to this screen again the data loads. The data is present in the packages which I get from reducers but it does not get assigned to the data state. Can anyone try checking and let me know what can be done here. Sorry if my code is not good. Thanks in advance.

Skipping code which is not necessary

JavaScript

data and filteredPackage both are empty at initial renders. I tried the below in useEffect but no luck

JavaScript

Please let me know if you need further details.

Advertisement

Answer

Make a const for packages and assign this to your data state

JavaScript

and write a useEffect that updates your data state when ever your package store value renders

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement