首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React子组件未在父状态更改时重新呈现

React子组件未在父状态更改时重新呈现
EN

Stack Overflow用户
提问于 2015-11-21 00:44:25
回答 3查看 4.4K关注 0票数 5

我是个新手,我在SO中查看了类似的问题,但没有找到任何问题。这可能是微不足道的,但请容忍我。

我有嵌套的组件

代码语言:javascript
复制
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方法:

代码语言:javascript
复制
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,下面是该组件的代码:

代码语言:javascript
复制
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>
    );
}

最后,该组件返回另一个组件,即根据SteepCardContainer组件中的数据动态生成的DIV元素。

代码语言:javascript
复制
 render () {

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

我从SteepCardContainer的状态通过props传递这些组件使用的所有数据。最初,当页面加载时,数据向下传递,state.data中的所有内容都用作indicator组件中的项,indicator组件是层次结构中的最后一个嵌套组件。所以,我的问题是,在状态改变时,props不会从steepcardcontainer组件更新。也就是说,状态改变了,我可以看到,但是属性并没有更新--特别是VisualisatioContainer没有更新它的属性,即使状态改变了。这就是我所面临的问题。任何帮助都是非常感谢的。

EN

回答 3

Stack Overflow用户

发布于 2015-11-21 00:57:26

我要做的第一件事就是。

代码语言:javascript
复制
 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>
    );
}

票数 1
EN

Stack Overflow用户

发布于 2015-11-21 01:07:24

我刚刚发现了问题所在,我让shouldComponentUpdate()在可视化组件中总是返回false。如果有任何人有类似的问题,希望这能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2015-11-21 00:57:58

我相信你把ES6类和React.createClass混在一起了。这是创建React组件的两种不同方式。

查看此处:https://facebook.github.io/react/docs/reusable-components.html#es6-classes

我有点惊讶,它居然还能运行。不同之处在于:

ES6类:

代码语言:javascript
复制
class HelloMessage extends React.Component {
  render() {
    return <div>Hello {this.props.name}</div>;
  }
}

**

React.createClass:

代码语言:javascript
复制
const HelloMessage = React.createClass({
  render: function() {
    return <div>Hello {this.props.name}</div>;
  }
});

解决这个问题,看看错误是否仍然存在。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33831817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档