0

I have a problem while using #react-hooks. When I dispatch an action with an onClick event, the "store state" is updated correctly. However, when I dispatch an action inside useEffect (It works like "componentDidMount"), state is not updated.

I've tried to debug and found out it seems like there is a problem with "Redux Dev Tools". (I consoled store state in component and it worked correctly).

Here is how I dispatch the action:

  useEffect(() => {
    onGetPosts();
  }, []);

My reducer:

const postPageReducer = (state = initialState, action) =>
  produce(state, draft => {
    switch (action.type) {
      case LOAD_POSTS_REQUEST:
        console.log('LOAD_POSTS_REQUEST');
        draft.load = true;
        draft.error = false;
        draft.posts = false;
        break;
      case LOAD_POSTS_SUCCESS:
        console.log('LOAD_POSTS_SUCCESS');
        draft.loading = false;
        console.log(action);
        draft.posts = action.posts;
        break;

      case POSTS_ERROR:
        console.log('POSTS_ERROR');
        draft.error = action.error;
        draft.loading = false;
        break;
    }
  });
HungDQ
  • 315
  • 1
  • 5
  • 15

0 Answers0