【发布时间】:2017-06-16 02:56:21
【问题描述】:
抱歉,我看到这个问题已被问过几次。我以前解决过这个问题,但我知道我正在以一种新的方式看待它。
这是我在助焊剂商店中的构造函数:
class TodoStore extends EventEmitter {
constructor() {
super()
this.state = {
todos: [
{
id: uuid(),
text: 'Go Shopping',
complete: false
},
{
id: uuid(),
text: 'Pay Water Bill',
complete: false
},
],
showCompletedTodos: true,
showIncompletedTodos: true
}
this.deleteTodo = this.deleteTodo.bind(this)
this.toggleTodo = this.toggleTodo.bind(this)
this.toggleShowCompletedTodos = this.toggleShowCompletedTodos.bind(this)
this.toggleShowIncompletedTodos = this.toggleShowIncompletedTodos.bind(this)
}
以下是我在子组件中通过 TodoActions.fctionName 调用的一些函数:
// Toggle the showing of completed todos
toggleShowCompletedTodos() {
this.setState({
showCompletedTodos: !this.state.showCompletedTodos
})
}
// Toggle the showing of incompleted todos
toggleShowIncompletedTodos() {
this.setState({
showIncompletedTodos: !this.state.showIncompletedTodos
})
}
deleteTodo(todoToDelete) {
this.setState({
todo: this.state.todos.filter( function(todo) {
return todo.id !== todoToDelete.id
})
})
}
当我触发这个函数时,我得到Uncaught TypeError: this.setState is not a function。
过去在构造函数中将函数绑定到this 解决了这个问题,但在这个问题上,它不起作用
这是我的参考: this.setState is not a function
是不是因为它是 Flux store 而不是普通类?
【问题讨论】:
-
你如何在父/子组件中传递/接收它们?
标签: reactjs reactjs-flux