Kar*_*eya 4 react-native redux react-redux
我已经实现了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);
Run Code Online (Sandbox Code Playgroud)
//减速器
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;
Run Code Online (Sandbox Code Playgroud)
我在最后得到一个错误,因为“ mapStateToProps必须接收一个普通对象,而不是接收到未定义的对象”。
请让我知道如何解决此问题。
您需要返回对象,您可以这样做,请参见对象周围的括号。
const mapStateToProps = state => ({
todos: state.todos
})
Run Code Online (Sandbox Code Playgroud)
你也可以
const mapStateToProps = state => {
return { todos: state.todos };
};
Run Code Online (Sandbox Code Playgroud)
两者都返回一个对象。
你有一个语法错误。您应该将代码括在括号中。
const mapStateToProps = state => ({
todos: state.todos
})
Run Code Online (Sandbox Code Playgroud)
简而言之:
const mapStateToProps = state => ( { } ) // This returns an empty object
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4531 次 |
最近记录: |