在React Redux中,Array方法filter不会删除项目。它会返回一个新的数组,其中包含满足给定条件的项目。
下面是一个示例代码,说明如何使用filter方法在React Redux中过滤项目:
import React from 'react';
import { connect } from 'react-redux';
const TodoList = ({ todos }) => {
// 过滤已完成的任务
const filteredTodos = todos.filter(todo => !todo.completed);
return (
{filteredTodos.map(todo => (
- {todo.text}
))}
);
};
const mapStateToProps = state => ({
todos: state.todos
});
export default connect(mapStateToProps)(TodoList);
在上面的代码中,我们使用filter方法过滤掉已完成的任务。在mapStateToProps函数中,我们将todos数组从Redux store中映射到TodoList组件的props中。然后,我们使用filter方法创建filteredTodos数组,其中只包含未完成的任务。最后,我们使用map方法将filteredTodos数组渲染为任务列表。
注意,filter方法不会直接修改原始数组,而是返回一个新的数组。如果你希望删除Redux store中的项目,你需要在Redux action中使用其他方法,如splice或slice。