【发布时间】:2018-10-01 12:14:58
【问题描述】:
我已经实现了 redux 来完成一个 TODO 应用程序。
我的待办事项代码
import { connect } from "react-redux";
import TodoList from "../components/TodoList.js";
// Here we will connect
const mapStateToProps = state => {
todos: state.todos
}
const mapDispatchToProps = dispatch => {
toggleTodo: id => dispatch({ type: 'TOGGLE_TODOS', id })
}
export default connect(mapStateToProps, mapDispatchToProps)(TodoList);
// 减速器
const todos = (state = [], action) => {
switch (action.type) {
case 'ADD_TODO':
return [
...state, {
id: action.id,
text: action.text,
visibility: false
}
]
case 'TOGGLE-TODO':
return state.map(todo => (todo.id === action.id)
?
{ ...todo, completed: !todo.completed }
: todo)
default:
return state;
}
}
export default todos;
最后我得到一个错误 'mapStateToProps 必须接收普通对象,而不是接收未定义'。
请告诉我如何解决这个问题。
【问题讨论】:
-
请将您的减速器添加到帖子中。
-
请检查更新的问题
-
您没有返回对象:
const mapStateToProps = state => ({ todos: state.todos });。注意括号。或者const mapStateToProps = state => { return { todos: state.todos }; };
标签: react-native redux react-redux