A
A
Alexey Yakovlev2020-11-15 14:51:38
React
Alexey Yakovlev, 2020-11-15 14:51:38

Error: Actions must be plain objects. Use custom middleware for async actions?

action:

export const signIn = valid => {
    return {
        type: 'SIGN_IN',
        payload: valid
    }
}


reducer:
const loggedReducer = (state=false, action) => {
    switch(action.type) {
        case 'SIGN_IN':
            return !state;
        default:
            return state;
    }
}

export default loggedReducer;


How I work with them:
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

1 answer(s)
A
Alexey Yakovlev, 2020-11-16
@aleshaykovlev

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 question

Ask a Question

731 491 924 answers to any question