首页 > 解决方案 > React 中的嵌套子组件 + 渲染新数据

问题描述

需要帮助,我目前正在学习反应,在创建博客应用程序时遇到了某种问题。

我将组件相互嵌套,但是除非我刷新嵌套组件的页面,否则子组件中不会收到更新的数据。下面是我的代码片段在 AccountOverview 组件中是另一个处理表单的组件(容器)。一旦表单提交数据并且后端处理数据,我希望 AccountOverview 组件将呈现新信息。但是,如果我随后返回父级(仪表板)并进行编辑,则 AccountOverview 组件不会显示新创建的数据,除非是在整页刷新时。

class Dashboard extends Component {
    state = {}
    componentDidMount(){
        this.props.getCurrentProfile();
    }
    render() {
        const { user } = this.props.auth;
        const { profile, loading } = this.props.profile;        
        return (
            <div style={{margin: "0", padding: "0"}}>
                <Header title="Account Dashboard" />
                <section id="accountPage" className="">
                <div className="container">
                  <div className="row">
                    <Sidebar />
                            <main className="col-sm-9 col-md-9 account">
                                <div className="dashboard">
                                    <AccountOverviewPanel 
                                        user={user} profile={profile} loading={loading}
                                    />
                                    <PostTablePanel />
                                </div>
                            </main>
                  </div>
                </div>
              </section>
            </div>
        );
    };
};

标签: reactjs

解决方案


您的提交操作是否会导致您的组件卸载。我尝试克隆您的代码。它似乎错过了密钥文件,因此无法运行它


推荐阅读