我在标题中发布了错误。我试图将一些代码放入通量模式,所以我有:
单击Todos.js中的按钮时,它会调用Todos.js中的createTodo()函数并执行
发生错误 - > TodoActions.createTodo不是Todos.createTodo的函数
Todos.js
import React from "react";
import Todo from "../components/Todo";
import TodoStore from "../stores/TodoStore";
import * as TodoActions from "../actions/TodoActions";
export default class Todos extends React.Component {
constructor() {
super();
this.state = {
todos: TodoStore.getAll(),
};
}
componentWillMount() {
TodoStore.on("change", () =>{
this.setState({
todos: TodoStore.getAll(),
});
});
}
createTodo(){
TodoActions.createTodo('x');
}
render() {
const { todos } = this.state;
const TodoComponents = todos.map((todo) => {
return <Todo key={todo.id} {...todo}/>;
});
return (
<div>
<button onClick={this.createTodo.bind(this)}>Create!</button>
<h1>Todos</h1>
<ul>{TodoComponents}</ul>
</div>
);
}
}
TodoActions.js
import dispatcher from "../thedispatcher/dispatcher";
export function createTodo(text){
dispatcher.dispatch({
type: "CREATE_TODO",
text,
});
}
提前致谢!
答案 0 :(得分:0)
我认为文件 TodoActions 是导入而不是 TodoActions.js ,所以如果您不需要它或将其重命名,请将其删除,以便< strong> TodoActions.js &#39;将被导入。