J
J
jjsplash2022-04-12 01:39:24
React
jjsplash, 2022-04-12 01:39:24

Is it possible to reset the state in a reducer like this?

Hello! Will it be correct in the reducer to return the state to its initial value in this way?

let initialState = {
    emailText: '',
    passText: '',
    user: {},
    isAuth: false,
};

const authReducer = (state = initialState, action) => {
    switch (action.type) {
        case LOGOUT: {
            return {
              state: initialState,
            }
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
black1277, 2022-04-12
@jjsplash

Strictly speaking, it's incorrect. From the definition of a reducer, it must be a pure function. A pure function should only take data from its arguments and have no side effects.
If you use an object defined outside the function (initialState - even if it lies nearby) - it ceases to be pure. Although it will work, but perhaps all sorts of linters can swear, issue a warning when compiling.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question