调度没有'无法立即更新组件

Dispatch doesn't update component instantly

本文关键字:更新 组件 调度      更新时间:2023-09-26

为了参考,我正在学习本教程。我的试用版应用程序是一个用户管理工具。当前在下面显示用户列表的是商店代码。它使用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;

编辑我想我需要触发状态更改吗?

要重新渲染数据,您应该通过调用setState或使用forceUpdate 强制渲染来触发渲染

请注意,不建议使用forceUpdate,组件读取存储数据的正确方法是将数据复制到状态中,然后在渲染函数中从此状态读取。

http://blog.andrewray.me/flux-for-stupid-people/