出现错误 "The slice reducer for key "weathReducer“在我的 expo 应用程序初始化期间返回未定义”

getting Error "The slice reducer for key "weathReducer" returned undefined during intialization in my expo app"

运行 进入错误:键“weatherReducer”的切片缩减器 return 在初始化期间未定义。如果传递给 reducer 的状态未定义,则必须显式 return 初始状态。初始状态可能不是未定义的。如果不想给这个reducer设置值,可以用null代替undefined。

at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:172:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/@react-native/polyfills/error-guard.js:49:36 in ErrorUtils.reportFatalError
at node_modules/metro-runtime/src/polyfills/require.js:204:6 in guardedLoadModule
at http://192.168.0.12:19000/node_modules/expo/AppEntry.bundle?platform=android&dev=true&hot=false&strict=false&minify=false:146332:3 in global code

通常在编写 reducer 函数时,会有一个初始化调用,它会在没有 action 参数的情况下调用 reducer。如果你在那个电话中,你必须 return initialState.

var initialState = {
  temperature: undefined,
  units: 'celsius',
  isLoading: false,
}

function weatherReducer(state, action) {
  switch(action) {
    case 'SET_TEMPERATURE': 
      return { ...state, temperature: action.temperature }
    // ... 
    default: 
      return state || initialState // <- here
  }
}