英文:
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
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
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论