@dev-anurag-singh
Posted
Hey, Richard I have read your code on GitHub and tried to explain how you can use useReducer correctly
const initialState = {
loggedIn:false,
token:"",
bookmarkResults:"",
}
// reducer function gets access to the current state and action dispatched
function reducer(state,action){
switch (action.type) {
case 'login':
return { ...state, token: action.payload, loggedIn: true };
case 'logout':
return { ...state, token: "", loggedIn: false};
// you can create more actions for adding a bookmark or removing a bookmark
default:
throw new Error('unknown action');
}
}
// Invoke this function to update login state
function login(email,password){
// code to check email & password
// if email && password are correct
dispatch({ type: 'login', payload: token });
}
Marked as helpful