在我使用 Android Studio 设置的 React Native 应用程序中,我使用 redux-persist 来持久化状态,存储引擎是 AsyncStorage。在应用程序中,我有一个“添加”按钮和一个“删除”按钮,用于添加和删除状态中的项目。
“添加”按钮工作正常,它将一个项目添加到状态并重新呈现屏幕。但是“删除”按钮虽然从状态中删除了一个项目(我在 React Native Debugger 上看到过),但它不会重新渲染屏幕并保持状态。但是不同版本的代码有效。
我想知道为什么以前的版本不起作用。这是我的减速器功能:
//reducer.js
import React from 'react';
const initialState = {
key: [
{ id: 0 },
{ id: 1 }
]
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case 'SAVE':
return { key: [...state.key, action.payload] };
// this version doesn't re-render the screen and doesn't persist
case 'REMOVE':
let { key } = state;
key.pop();
return { key };
// this version re-renders the screen and persists
case 'REMOVE':
let { key } = state;
return { key : key.slice(0, key.length - 1)};
default:
return state;
}
};
export { reducer };
宝慕林4294392
富国沪深
相关分类