Search code examples
reactjsreduxnext.jsredux-thunk

Why is the correct action not triggering while dispatching?


Action

export const VERIFY = () => dispatch => {
    dispatch({type: "VERIFY"})
};

Reducer

const signedReducer = (state=user, action) => {
    console.log(action);

    switch(action.type){
     
        case "VERIFY": {
            return {...state, email: "example@gmail.com"};
        }
        default: {
            return state;
        }
    }

}

_app.js code

import { wrapper } from '../redux/store';

function MyApp({ Component, pageProps }) {

  return <>
    <Component {...pageProps}/>
  </>
}

MyApp.getInitialProps = async(appContext) => {
  let { pageProps } = appContext
    pageProps = {};
    if(appContext.Component.getInitialProps){
     
      pageProps = await appContext.Component.getInitialProps(appContext.ctx);
    }

    return {
      pageProps,
 
    };
};

export default wrapper.withRedux(MyApp);

& finally pages/home.js

import { useEffect } from "react";
import PrivateLayout from "../components/PrivateLayout/PrivateLayout";
import { connect } from "react-redux";
import { VERIFY } from "../redux/actions/signActions";

function Home() {
  // console.log(user);

  // useEffect(() => {

  // }, [user]);

  
  return (
      <div >
        { true ? 
            <h1>Logged In</h1> 
                  : 
            <h1>Please login again</h1>
        }
      </div>
  )
}

const mapStateToProps = state => ({
  user: state
})

const mapDispatchToProps = {
  VERIFY: VERIFY
}

export default connect(mapStateToProps, mapDispatchToProps)(Home);

Please check, I've put an console.log statement in the reducer. Whenever I run the code the console.log statement display these action type only

  • @@redux/INIT6.z.d.a.h.7
  • @@redux/PROBE_UNKNOWN_ACTIONq.x.h.3.5.d

But never takes the action VERIFY. Looked across the internet but haven't found any solution regarding this. Why?


Solution

  • This should solve your problem:

    Also you need to call VERIFY in your component.

    Action:

    export const VERIFY = () => ({type: "VERIFY"});
    

    reference: https://react-redux.js.org/using-react-redux/connect-mapdispatch