1
供參考我正在關注this教程。我的試用版應用程序是用戶管理工具。其中目前顯示的是下面的用戶列表是商店代碼。它成功地使用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: '[email protected]'
},
{
id: 12345,
name: 'Bacon',
email: '[email protected]'
}
];
}
getAll(){
return this.users;
}
createUser(name){
const id = Date.now();
this.users.push({
id,
name,
email: '[email protected]'
});
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;
編輯我想我需要觸發的狀態變化?
感謝您的確認。我檢查了教程git repo並查看了代碼,發現他沒有包含在視頻教程中的setState命令。感謝您重申我的懷疑。 – Tony