这是我的代码
export const getAsyncStorage = () => async dispatch => {
try {
const asyncStorage = await AsyncStorage.getItem('settings');
const asyncStorageObject = asyncStorage != null ? JSON.parse(asyncStorage) : null;
console.log(asyncStorageObject) // this logs an object, as expected
dispatch({ type: GET_ASYNC_STORAGE, payload: asyncStorageObject })
} catch (e) {
console.log('Error in getAsyncStorage writing to AsyncStorage', e)
}
}
这是我得到的错误
getAsyncStorage写入AsyncStorage时出错参考错误:payload is not defined
为什么会抛出"有效载荷未定义"?这对我来说毫无意义。删除这一行
dispatch({ type: GET_ASYNC_STORAGE, payload: asyncStorageObject })
解决它。当我改变这一行
dispatch({ type: GET_ASYNC_STORAGE, payload: asyncStorageObject })
dispatch({ type: GET_ASYNC_STORAGE, foo: asyncStorageObject })
我仍然得到相同的payload
消息…不知道那是什么意思。以下是我给AsyncStorage
写的:
export const writeAsyncStorage = () => async dispatch => {
try {
const settings = {...}
await AsyncStorage.setItem('settings', JSON.stringify(settings));
dispatch({ type: WRITE_ASYNC_STORAGE, payload: true, })
} catch (e) {
console.log('Error in onIntroDone writing to AsyncStorage', e)
}
}
和reducer
import {
GET_ASYNC_STORAGE,
...
} from '../constants/actions';
const initialState = {
...
asyncStorage: {}
};
const settings = (state = initialState, action) => {
switch (action.type) {
...
case GET_ASYNC_STORAGE:
return {
...state,
asyncStorage: payload.asyncStorage
}
default:
return state;
}
}
export default settings;
在您的减速器中,您必须键入action.payload
而不是payload.asyncStorage