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

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

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:

  1. import { applyMiddleware, combineReducers, createStore } from "redux"
  2. import thunk from "redux-thunk"
  3. import {composeWithDevTools} from "redux-devtools-extension"
  4. import authReducer from "./reducers/auth"
  5. const initialState = {
  6. }
  7. const reducers = combineReducers({
  8. auth: authReducer
  9. })
  10. const store = createStore(reducers,initialState, composeWithDevTools(applyMiddleware(thunk)))
  11. export default store

and here is my auth reducer:

  1. const authReducer = (state = {auth:null}, action) =>{
  2. switch (action.type) {
  3. case "REGISTER":
  4. localStorage.setItem('auth',JSON.stringify(action.payload))
  5. return{
  6. ...state,
  7. auth:action.payload
  8. }
  9. case "LOGIN":
  10. localStorage.setItem('auth',JSON.stringify(action.payload))
  11. return{
  12. ...state,
  13. auth: action.payload
  14. }
  15. case "LOGUT":
  16. localStorage.clear()
  17. return{
  18. ...state,
  19. auth: null
  20. }
  21. default:
  22. break;
  23. }

}

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.

  1. default:
  2. return state;
英文:

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

  1. default:
  2. return state;

答案2

得分: 1

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

英文:

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

  1. switch (action.type) {
  2. // ...
  3. }
  4. return state;

答案3

得分: 1

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

  1. switch (action.type) {
  2. case "REGISTER":
  3. localStorage.setItem('auth', JSON.stringify(action.payload))
  4. return {
  5. ...state,
  6. auth: action.payload
  7. }
  8. case "LOGIN":
  9. localStorage.setItem('auth', JSON.stringify(action.payload))
  10. return {
  11. ...state,
  12. auth: action.payload
  13. }
  14. case "LOGOUT":
  15. localStorage.clear()
  16. return {
  17. ...state,
  18. auth: null
  19. }
  20. default:
  21. return state;
  22. }
英文:

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

  1. switch (action.type) {
  2. case "REGISTER":
  3. localStorage.setItem('auth',JSON.stringify(action.payload))
  4. return{
  5. ...state,
  6. auth:action.payload
  7. }
  8. case "LOGIN":
  9. localStorage.setItem('auth',JSON.stringify(action.payload))
  10. return{
  11. ...state,
  12. auth: action.payload
  13. }
  14. case "LOGUT":
  15. localStorage.clear()
  16. return{
  17. ...state,
  18. auth: null
  19. }
  20. default:
  21. return state;
  22. }

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:

确定