Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
Can not navigate to another pages after login

i dont know where is the trouble. but when i tried to login, it dont wanna navigate into the next pages and stuck in modal login. please tell me whats wrong with my code?

let navigate = useNavigate();
const dispatch = useContext(UserContext);
const state = useContext(UserContext);
// console.log(state);
const [form, setForm] = useState({
  email: "",
  password: "",
});

const { email, password } = form;
const handleChange = (e) => {
  setForm({
    ...form,
    [e.target.name]: e.target.value,
  });
};

const handleSubmitLog = async (e) => {
  try {
    e.preventDefault();
    const config = {
      headers: {
        "Content-type": "application/json",
      },
    };
    const body = JSON.stringify(form);
    const response = await API.post("/login", body, config);
    console.log(response.data);

    if (response?.status == 200) {
      dispatch({
        type: "LOGIN_SUCCESS",
        payload: response.data.data,
      });
      
      if (response.data.data.status == "admin") {
        navigate('/admin')
      } else {
        navigate('/userid')
      }
    }
  } catch (error) {
    console.log(error);
  }
}

here is the response in console, i dont know why this dispatch can not work well

{
    "status": "Success",
    "data": {
        "users": {
            "name": "mr.x",
            "email": "mr.x@mail.com",
            "token": "asfsa"
        }
    }
}

TypeError: dispatch is not a function
    at handleSubmitLog (header.js:59:1)

and here is the body from userContext file. please check it and tell me if my code is wrong

export const UserContext = createContext();

const initialState = {
  isLogin: false,
  user: {},
};

const reducer = (state, action) => {
  const { type, payload } = action;

  switch (type) {
    case "USER_SUCCESS":
    case "LOGIN_SUCCESS":
      localStorage.setItem("token", payload.token)
      return {
        isLogin: true,
        user: payload,
      };
    case "AUTH_ERROR":
    case "LOGOUT":
      localStorage.removeItem("token")
      return {
        isLogin: false,
        user: {},
      };
    default:
      throw new Error();
  }
};

export const UserContextProvider = ({ children }) => {
  const [state, dispatch] = useReducer(reducer, initialState);

  return (
    <UserContext.Provider value={[state, dispatch]}>
      {children}
    </UserContext.Provider>
  );
};
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The UserContext value is an array [state, dispatch]:

<UserContext.Provider value={[state, dispatch]}>
  {children}
</UserContext.Provider>

But the component is not correctly accessing the context value:

const dispatch = useContext(UserContext);
const state = useContext(UserContext);

Here both dispatch and state have are the same context value of [state, dispatch].

You need only one useContext(UserContext) access, either of the following:

  • Save the entire context value and use array indexing:

     const userContext = useContext(UserContext);
    
     ...
    
     // userContext[0]; state object/value
     // userContext[1]; dispatch function
    
     ...
    
     userContext[1]({
       type: "LOGIN_SUCCESS",
       payload: response.data.data,
     });
    
  • Save the state and dispatch values directly using array destructuring assignment:

     const [state, dispatch] = useContext(UserContext);
    
     ...
    
     dispatch({
       type: "LOGIN_SUCCESS",
       payload: response.data.data,
     });
    
about 4 years ago · Juan Pablo Isaza Report

0

Try this:

  const { state, dispatch } = useContext(AppContext)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!