作为参考,我遵循这教程。我的试用应用是一个用户管理工具。下面显示的用户列表是存储代码。它成功地使用dispatcher.dispatch({type: "CREATE_USER", name: "Andrew"})将用户添加到列表中。然而,它不会这样做,直到我点击一个路由,然后它更新。
import { EventEmitter } from "events";
import dispatcher from "../Dispatcher";
class UserManagement extends EventEmitter{
constructor(){
super();
this.users =
[
{
id: 1234,
name: 'Anton',
email: 'escalante.anton@outlook.com'
},
{
id: 12345,
name: 'Bacon',
email: 'bacon@me.com'
}
];
}
getAll(){
return this.users;
}
createUser(name){
const id = Date.now();
this.users.push({
id,
name,
email: 'default@email.com'
});
this.emit("change");
}
handleActions(action){
switch(action.type){
case "CREATE_USER":{
this.createUser(action.name);
break;
}
}
}
}
const userobj = new UserManagement;
dispatcher.register(userobj.handleActions.bind(userobj));
window.dispatcher = dispatcher;
export default userobj;编辑我认为我需要触发状态更改?
发布于 2016-05-18 06:14:04
若要重新呈现数据,应通过调用setState或强制使用forceUpdate进行呈现来触发呈现。
注意,forceUpdate是不推荐的,组件读取存储数据的正确方法是将数据复制到状态并在呈现函数中从this.state读取。
http://blog.andrewray.me/flux-for-stupid-people/
https://stackoverflow.com/questions/37291311
复制相似问题