reactjs - 如何在 redux 中获得以前的状态?
问题描述
我在使用 react 和 redux 制作简单的天气应用程序时遇到了麻烦。
我有两个减速器。一种用于将数据添加到数组中,另一种用于使用 find 方法从数组中删除数据。
这是代码:
// actions
// fetching weather api
export const fetchWeather = term => {
return async dispatch => {
const res = await api.get(
`/data/2.5/weather?q=${term}&APPID=132123123132`
);
dispatch({ type: "FETCH_WEATHER", payload: res.data });
};
};
// delete a list that matches the selected id
export const listDelete = id => {
return {
type: "LIST_DELETE",
paylod: id
};
};
// reducers
// reducer that put a new data into an array
const reducerIndex = (state = [], action) => {
switch (action.type) {
case "FETCH_WEATHER":
return [...state, action.payload];
default:
return state;
}
};
// reducer that deleting the data that id selected
const reducerIndexDel = (state = [], action) => {
switch (action.type) {
case "LIST_DELETE":
return state.find(city => city.id !== action.payload);
default:
return state;
}
};
export default combineReducers({
reducerIndex,
reducerIndexDel
});
您可能知道,浏览器给我一个错误,说“给定操作“LIST_DELETE”,reducer“reducerIndexDel”返回未定义。
我能够获取我想要的数据,但是我怎样才能让 reducerIndexDel 可以识别以前的状态?
解决方案
仅用于filter
获取不包含带有 id 的城市的状态。
return state.filter(city => city.id !== action.payload)
推荐阅读
- arrays - 使用 scala 将 JSON 对象字段添加到数据框中的 JSON 数组字段
- c# - Autofac 和 DI - 如何用 UnitOfWork 解决这个问题
- tkinter - 创建新框架时未清除 columnconfigure 和 rowconfigure
- php - 从 PHP 中的外键引用中获取列值
- java - Spring Boot如何理解URL是字符串?
- python - crontab 计划未运行
- python - ValueError:无法使用此命令获取 Chrome 的版本:reg query
- python - scipy.optimize_scalar() 给出“UnboundLocalError:分配前引用的局部变量 'fu'”
- c - 这个初始化有什么问题?为什么我尝试使用初始化函数初始化图形时无法成功
- java - 对于不完全为 4 位数的年份,JPA 年份验证抛出异常