简体   繁体   English

状态更改时子组件的道具不更新

[英]Child component's props not updating when state changes

I'm using drag and drop to instantiate react classes, but for some reason the state from the parent component is not being passed to the child. 我正在使用拖放实例化React类,但是由于某些原因,父组件的状态没有传递给子组件。 The child isn't even being rerendered, tried shouldComponentUpdate and componentWillReceiveProps. 甚至没有重新渲染孩子,请尝试shouldComponentUpdate和componentWillReceiveProps。

Parents relevant code: 家长相关代码:

dragEnd(e) {
    e.preventDefault();
    let me = this;

    let el = (
        <Pie key={ Date.now() * Math.random() } yAxisData={ me.state.yAxisData } legendData={ me.state.legendData } />
    )

    this.setState({
        cells: this.state.cells.concat(el),
    });
}

So, on drop, is created, and then render looks like: 因此,一经创建,然后呈现如下所示:

render() {
    <div className = { "insights-data" } onDrop={ this.dragEnd } onDragOver={ this.preventDefault }>
        { this.state.cells }
    </div>
}

All this works fine, but now when I change the data passed to this.state.yAxisData and/or this.state.legendData, it's not calling render on the child component. 所有这些工作正常,但是现在当我更改传递给this.state.yAxisData和/或this.state.legendData的数据时,它没有在子组件上调用render。

Here's the child components render: 这是子组件的渲染:

    render() {
    return (
        <div className="insights-cell">
            <ReactECharts
                option={ this.create() }
                style={{ position: "absolute", top: 0, bottom: 0, left: 0, right: 0, height: "100%" }}
                theme="chalk"
                notMerge={ true }
            />
        </div>
    )
}

Any ideas? 有任何想法吗? I thought maybe there was a binding issue, but that doesn't seem to be it, as I'm using me = this. 我以为可能存在一个有约束力的问题,但事实并非如此,因为我正在使用me = this。 It's not even re-rendering the child component. 它甚至没有重新渲染子组件。

You are already creating the element in dragEnd function by passing props to it and storing them to an array. 您已经通过将dragEnd传递给dragEnd函数并将其存储到数组中来创建该元素。 Therefore the array this.state.cells contain the array of already declared elements. 因此,数组this.state.cells包含已声明的元素的数组。 Therefore it cannot update on state change. 因此,它无法根据状态更改进行更新。 You should render a new array of element on every render. 您应该在每个渲染器上渲染一个新的元素数组。

Just push some necessary detail of dragged element in this.state.cells and then iterate through this array on every render. 只需在this.state.cells推送拖动元素的一些必要细节,然后在每个渲染器上迭代此数组。

dragEnd(e) {
    e.preventDefault();

    let el = draggedElementType

    this.setState({
        cells: this.state.cells.concat(el),
    });
}

And in render, iterate through this array and return the desired element. 在渲染中,迭代此数组并返回所需的元素。

render() {
    <div className = { "insights-data" } onDrop={ this.dragEnd } onDragOver={ this.preventDefault }>
        { this.state.cells.map((cell, index) => {
           if (cell === "pie") {
           return (<Pie key={index} yAxisData={ me.state.yAxisData } legendData={ me.state.legendData } />);
           }
           else if (){...
        )}
    </div>
}

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

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