Answer the question
In order to leave comments, you need to log in
Error: Actions must be plain objects. Use custom middleware for async actions?
action:
export const signIn = valid => {
return {
type: 'SIGN_IN',
payload: valid
}
}
const loggedReducer = (state=false, action) => {
switch(action.type) {
case 'SIGN_IN':
return !state;
default:
return state;
}
}
export default loggedReducer;
const [username, setUsername] = React.useState('');
const [password, setPassword] = React.useState('');
const dispatch = useDispatch();
localStorage.setItem('user', JSON.stringify(dispatch(signIn(false)).payload));
function login(event) {
event.preventDefault();
if (username === 'Admin') {
if (password === '12345') {
localStorage['user'] = dispatch(signIn(true).payload);
}
} else {
localStorage['user'] = dispatch(signIn(false).payload);
alert('Имя пользователя или пароль введены не верно');
}
}
Answer the question
In order to leave comments, you need to log in
You need to add JSON.stringify()
and take payload from dispatch
localStorage['user'] = JSON.stringify(dispatch(signIn(true)).payload);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question