如果传递给 reducer 的状态未定义,必须明确返回初始状态。

huangapple go评论54阅读模式
英文:

If the state passed to the reducer is undefined,you must explicitly return the initial state

问题

以下是您要翻译的部分:

"I'm new on react redux and i got an error like on the below
如果传递给 reducer 的状态未定义,必须明确返回初始状态。

and here is my store.js:

import { applyMiddleware, combineReducers, createStore } from "redux"
import thunk from "redux-thunk"
import {composeWithDevTools} from "redux-devtools-extension"
import authReducer from "./reducers/auth"

const initialState = {

}

const reducers = combineReducers({
auth: authReducer
})

const store = createStore(reducers,initialState, composeWithDevTools(applyMiddleware(thunk)))

export default store"

and here is my auth reducer:

const authReducer = (state = {auth:null}, action) =>{

switch (action.type) {
case "REGISTER":
localStorage.setItem('auth',JSON.stringify(action.payload))
return{
...state,
auth:action.payload
}
case "LOGIN":
localStorage.setItem('auth',JSON.stringify(action.payload))
return{
...state,
auth: action.payload
}
case "LOGUT":
localStorage.clear()
return{
...state,
auth: null
}
default:
break;
}

}

export default authReducer

I wanted to take a login form datas but i got error like this. If anyone can help me I will appreciate"

英文:

I'm new on react redux and i got an error like on the below
如果传递给 reducer 的状态未定义,必须明确返回初始状态。

and here is my store.js:

import { applyMiddleware, combineReducers, createStore } from "redux"
import thunk from "redux-thunk"
import {composeWithDevTools} from "redux-devtools-extension"
import authReducer from "./reducers/auth"


const initialState = {

}


const reducers = combineReducers({
    auth: authReducer
})

const store = createStore(reducers,initialState, composeWithDevTools(applyMiddleware(thunk)))


export default store

and here is my auth reducer:

const authReducer = (state = {auth:null}, action) =>{

switch (action.type) {
    case "REGISTER":
        localStorage.setItem('auth',JSON.stringify(action.payload))
        return{
            ...state,
            auth:action.payload
        }
    case "LOGIN":
        localStorage.setItem('auth',JSON.stringify(action.payload))
        return{
            ...state,
            auth: action.payload
        }
    case "LOGUT":
        localStorage.clear()
        return{
            ...state,
            auth: null
        }
    default:
        break;
}

}

export default authReducer

I wanted to take an login form datas but i got error like this. If anyone can help me I will appreciate

答案1

得分: 2

I think you need to return state in default so it doesn't return undefined.

default:
    return state;
英文:

I think you need to return state in default so it doesn't return undefined.

default:
    return state;

答案2

得分: 1

如果没有匹配的动作,请在最后返回状态本身。

英文:

You should return the state itself at the end if no action matched.

switch (action.type) {
    // ...
}
return state;

答案3

得分: 1

我认为你应该尝试编辑你的 switch 的默认值,像这样:

switch (action.type) {
    case "REGISTER":
        localStorage.setItem('auth', JSON.stringify(action.payload))
        return {
            ...state,
            auth: action.payload
        }
    case "LOGIN":
        localStorage.setItem('auth', JSON.stringify(action.payload))
        return {
            ...state,
            auth: action.payload
        }
    case "LOGOUT":
        localStorage.clear()
        return {
            ...state,
            auth: null
        }
    default:
        return state;
}
英文:

I think you should try edit your default of switch like:

switch (action.type) {
    case "REGISTER":
        localStorage.setItem('auth',JSON.stringify(action.payload))
        return{
            ...state,
            auth:action.payload
        }
    case "LOGIN":
        localStorage.setItem('auth',JSON.stringify(action.payload))
        return{
            ...state,
            auth: action.payload
        }
    case "LOGUT":
        localStorage.clear()
        return{
            ...state,
            auth: null
        }
    default:
        return state;
}

huangapple
  • 本文由 发表于 2023年5月24日 19:04:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定