"The slice reducer for key "减速器" returned undefined during intialization in my expo app"获取错误



running into Error: slice reducer for key "weatherReducer"初始化期间返回未定义。如果传递给reducer的状态未定义,则必须显式返回初始状态。初始状态可能不是未定义的。如果你不想为这个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参数。如果你在那个调用中,你必须返回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
}
}