【问题标题】:React child component not re-rendering on parent state change反应子组件不会在父状态更改时重新渲染
【发布时间】:2016-02-23 05:51:15
【问题描述】:

我是 React 新手,查看了 SO 中的类似问题,但找不到任何问题。这可能微不足道,但请多多包涵。

我有嵌套组件

const IndicatorsSteepCardContainer = React.createClass({
getInitialState () {

    return {
        steep: {
            Social: true,
            Tech: true,
            Economy: true,
            Ecology: true,
            Politics: true
        }, 

        data: indicatorData
    }
}

 render () {

    let props = this.props;
    let state = this.state;
    console.log('STATE inside steepcardcontainer >>>>', this.state.data);
    // VisualisationContainer to have different data eventually
    return (
        <div className="indicators">
            <h3 className="description-text">Tap on an issue group below to filter indicators available in the data visualisation. Tap it again to return to the full list of indicators.</h3>
            <div className='steep container steep-container'>
                <SteepCard selected={ this.selectedSteeps } steep="Social" data={ props.data.themeData } class="yellow" title="Social" steepIcon={ SocialIcon } />
                <SteepCard selected={ this.selectedSteeps } steep="Tech" data={ props.data.themeData } class="blue" title="Tech" steepIcon={ TechIcon }/>
                <SteepCard selected={ this.selectedSteeps } steep="Economy" data={ props.data.themeData } class="pink" title="Economy" steepIcon={ EconomyIcon } />
                <SteepCard selected={ this.selectedSteeps } steep="Ecology" data={ props.data.themeData } class="green" title="Ecology" steepIcon={ EcologyIcon } />
                <SteepCard selected={ this.selectedSteeps } steep="Politics" data={ props.data.themeData } class="orange" title="Politics" steepIcon={ PoliticsIcon }  />
            </div>
            <VisualisationContainer data={this.state.data}/>
        </div>
    );
}
});

这是 VisualisationContainer 组件中的渲染方法:

render () {

  console.log('props inside visualisation-container>>>', this.props.data);

    return (
        <div className='visualisation-container'>
            <div className="render-button" onTouchTap= { this.d3It.bind(null, this.selectedSteeps) }>
                Plot graph
            </div>
            <div className="update-button" onTouchTap= { this.update.bind(null, this.selectedSteeps) }>
                Update
            </div>
            <div className="indicator-items">
                <IndicatorList data={this.props.data} className="indicator-list" />
                <SelectedIndicators visible={true} onClick={this.toggleVisibility}/>
            </div>

        </div>
    );
}

该组件内部还有另一个嵌套组件,称为 IndicatorList,这是该组件的代码:

render () {

    let props = this.props;
    let keys = Object.keys(props.data);
    console.log('props inside indicator-list>>>', props.data);
    let allIndicators = [];

    keys.forEach(function(key){
        var indicators = Object.keys(props.data[key]); 
        allIndicators.push(indicators);

        // console.log(allIndicators);

        });

    let merged = [].concat.apply([], allIndicators);

    return (
        <div className="indicator-list">

            {
                merged.map((val, i) => {
                    return <Indicator className="indicator-name" key={i} value={val} />
                })
            }
        </div>
    );
}

最后,这个组件返回另一个组件,它是简单的 DIV 元素,是根据 SteepCardContainer 组件中的数据动态生成的。

 render () {

    return (
        <div> { this.props.value }</div>
    );
}

我通过来自 SteepCardContainer 状态的道具传递这些组件使用的所有数据。最初,当页面加载时,数据被向下传递,state.data 中的所有内容都用作指示器组件中的项目,这是层次结构中的最后一个嵌套组件。所以,我的问题是,在状态更改时,道具不会从陡卡容器组件中更新。即状态发生变化,我可以看到,但是道具没有更新 - 特别是 VisualisatioContainer 没有更新其道具,即使状态发生了变化。这就是我遇到的问题。很感谢任何形式的帮助。

【问题讨论】:

  • 第一个组件的 getInitialStaterender 之间是否缺少逗号 (,)?

标签: javascript reactjs


【解决方案1】:

我要做的第一件事就是。

 render () {

    let propsdThemeData = this.props.data.themeData;
    let stateData = this.state.data;
    console.log('STATE inside steepcardcontainer >>>>', this.state.data);
    // VisualisationContainer to have different data eventually
    return (
        <div className="indicators">
            <h3 className="description-text">Tap on an issue group below to filter indicators available in the data visualisation. Tap it again to return to the full list of indicators.</h3>
            <div className='steep container steep-container'>
                <SteepCard selected={ this.selectedSteeps } steep="Social" data={ propsdThemeData } class="yellow" title="Social" steepIcon={ SocialIcon } />
                <SteepCard selected={ this.selectedSteeps } steep="Tech" data={ propsdThemeData } class="blue" title="Tech" steepIcon={ TechIcon }/>
                <SteepCard selected={ this.selectedSteeps } steep="Economy" data={ propsdThemeData } class="pink" title="Economy" steepIcon={ EconomyIcon } />
                <SteepCard selected={ this.selectedSteeps } steep="Ecology" data={ propsdThemeData } class="green" title="Ecology" steepIcon={ EcologyIcon } />
                <SteepCard selected={ this.selectedSteeps } steep="Politics" data={ propsdThemeData } class="orange" title="Politics" steepIcon={ PoliticsIcon }  />
            </div>
            <VisualisationContainer data={stateData}/>
        </div>
    );
}

【讨论】:

    【解决方案2】:

    我刚刚发现问题出在哪里,我的 shouldComponentUpdate() 总是在可视化组件中返回 false。如果有人遇到类似问题,希望这会有所帮助。

    【讨论】:

    • 很高兴您找到了答案。但是,请注意我的回复并修复您的代码 - 这很重要。
    【解决方案3】:

    我相信您正在混合使用 ES6 类和 React.createClass。这是创建 React 组件的两种不同方式。

    请看这里:https://facebook.github.io/react/docs/reusable-components.html#es6-classes

    我有点惊讶这个运行。 区别如下:

    ES6 类:

    class HelloMessage extends React.Component {
      render() {
        return <div>Hello {this.props.name}</div>;
      }
    }
    

    **

    React.createClass:

    const HelloMessage = React.createClass({
      render: function() {
        return <div>Hello {this.props.name}</div>;
      }
    });
    

    修复它并查看错误是否仍然存在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 2019-04-03
      • 2015-11-03
      相关资源
      最近更新 更多