0

I want to convert this Redux lifestate to Redux hooks. As we see it's easy but I am getting problem while I use dispatch instead of setState().

 async componentDidMount() {
    try {
      await Auth.currentSession();
      this.userHasAuthenticated(true);
    } catch (e) {
      if (e !== "No current user") {
        alert(e);
      }
    }

    this.setState({ isAuthenticating: false });
  }

converted to hooks

   const isAuthentication = useSelector(state => state.isAuthinticating);
   const dispatch = useDispatch();

   useEffect(() => {
    async function fetchData(){
       try {
      await Auth.currentSession();
      this.userHasAuthenticated(true);
    } catch (e) {
      if (e !== "No current user") {
        alert(e);
      }
    }
    }
   fetchData();
   // problem begins here
   dispatch(stateName({type:'SET_AUTH', payload: false}));
   }

i was unable to use

dispatch(stateName({type:'SET_AUTH', payload: false}));

inside of useEffect. is it there way to do this

skyboyer
  • 22,209
  • 7
  • 57
  • 64
  • Does this answer your question? [React hooks: dispatch action from useEffect](https://stackoverflow.com/questions/54930197/react-hooks-dispatch-action-from-useeffect) – goto Feb 09 '20 at 16:12
  • Shouldnt the dispatch be inside `fetchData`? This way `fetchData` would run the same code that `componentDidMount` does. – Alvaro Feb 10 '20 at 01:57

0 Answers0