Skip to content Skip to sidebar Skip to footer

Using The Same Data Dependently In The Same Useeffect

I need to fetch my data in two different ways and render it according to this. At the first load, I need to fetch all the items one by one and increment the count. After that, I ne

Solution 1:

I added init to your store:

// @dataReducer.jsexportconst initialDataState = {
  init: true,
  posts: []
};

constdataReducer = (state, action) => {
  switch (action.type) {
    case'ALL':
      // init falsereturn { ...state, posts: action.payload };
    case'INC':
      return { ...state, init: false, posts: [...state.posts, action.payload] };
...
}
// @App.jsfunctionApp() {
  const [{ init, posts }, dispatch] = useGlobalState();

  useEffect(() => {
    init ? getInc(dispatch) : getAll(dispatch);
  }, [init, dispatch]);
...
}

Edit funny-violet-pwew6

Post a Comment for "Using The Same Data Dependently In The Same Useeffect"