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.js
export const initialDataState = {
  init: true,
  posts: []
};

const dataReducer = (state, action) => {
  switch (action.type) {
    case 'ALL':
      // init false
      return { ...state, posts: action.payload };
    case 'INC':
      return { ...state, init: false, posts: [...state.posts, action.payload] };
...
}
// @App.js
function App() {
  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"