Home > Software design >  Check if a there is nothing in the redux store, and fill the store
Check if a there is nothing in the redux store, and fill the store

Time:06-30

I have selectedProject in my redux store which contains projects. However, when there is no project in the store, I would like to fill the project array with the lastOpenedProject's id.

here is what I have for now:

  const selectedProjectIsEmpty =()=>{
    if(selectedProject.length === 0){
        dispatch(selectedProject: projects.filter((el) => el.id === lastOpenProject))   
    }
}
selectedProjectIsEmpty()

CodePudding user response:

Which library do you use? Redux or Redux Toolkit?

CodePudding user response:

you cannot directly mutate redux states instead you dispatch an action which calls the appropriate reducer to update the state values,In your dispatch action function you are directly assigning value to the redux state instead create an action function which takes an array as an parameter and updates the redux state ,like this

Using redux toolkit :

....

   reducers : {

          setSelectedProjects : (state,{payload}) => {
              state.selectedProjects = payload;
          }
          }
          
....          

you always dispatch actions to update state!,dont mutate values!.

  • Related