简体   繁体   English

是否可以更新由React.createElement创建的React组件?

[英]Is it possible to update React component, that is created with React.createElement?

I have code, that creates dynamic table with dynamic number of rows - each row is React component (created by createElement ): 我有代码,创建具有动态行数的动态表-每行都是React组件(由createElement创建):

 render() {
        var rows = this.props.data.map(function(rowData) {
            return React.createElement(ScheduleRow, {
                data: rowData
            });
        });
        this.state.rows = rows;
        return (
            <div>
                <table>
                    {rows}
                </table>
            </div>
        );
    }

I can access each individual row by this.state.rows[rowNumber] , whose JSON stringify gives TypeError: Converting circular structure to JSON , that is fine, unfortunately I can not explore the structure of this object. 我可以通过this.state.rows[rowNumber]来访问每一行,其JSON字符串化提供TypeError: Converting circular structure to JSON ,这很好,不幸的是,我无法探索此对象的结构。

So - my question is - can I call some methods on this.state.rows[rowNumber] with aim to update the style, data of this component? 所以-我的问题是-我可以在this.state.rows[rowNumber]上调用一些方法来更新该组件的样式和数据吗? Or maybe I can even access the child components and update the style and data of some child component of this.state.rows[rowNumber] ? 或者,也许我什至可以访问子组件并更新this.state.rows[rowNumber]的某些子组件的样式和数据?

I don't know why do u need React.createElement in this context when you could just call the element in JSX form as in: { this.props.data.map(row => <ScheduleRow {...row} />) } 我不知道为什么您只需在JSX形式中调用元素就可以在这种情况下使用React.createElement ,例如: { this.props.data.map(row => <ScheduleRow {...row} />) }

But generally, a component is when it's props / state is updated. 但通常来说,组件是在props / state被更新时。 So when your this.props.data is modified the component shall rerender and update your child component. 因此,当您修改this.props.data时,组件将重新渲染并更新您的子组件。

NOTE : Do not set state using this.state = //whatever checkout this article: https://medium.com/@baphemot/understanding-reactjs-setstate-a4640451865b 注意 :请勿使用this.state = //whatever设置状态, this.state = //whatever检出本文: https : this.state = //whatever

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM