简体   繁体   English

React子组件不会在父状态更改时重新呈现

[英]React child component not re-rendering on parent state change

I am new to React, and looked through the similar questions in SO and couldn't find any. 我是React的新手,在SO中查看了类似的问题,找不到任何问题。 This maybe trivial but please bear with me. 这可能是微不足道的,但请耐心等待。

I have nested components 我有嵌套组件

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>
    );
}
});

Here is the render method in VisualisationContainer component: 这是VisualisationContainer组件中的render方法:

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>
    );
}

This component has another nested component inside it called IndicatorList, here is the code for that component: 该组件内部有另一个嵌套组件,名为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>
    );
}

Finally, this component returns another component which is simply DIV elements that are dynamically generated based on the data in SteepCardContainer component. 最后,该组件返回另一个组件,它只是基于SteepCardContainer组件中的数据动态生成的DIV元素。

 render () {

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

I am passing all the data used by these components via props from SteepCardContainer's state. 我通过SteepCardContainer状态的道具传递这些组件使用的所有数据。 Initially when the page is loaded the data is passed down and everything that is inside the state.data is used as the items in indicator component, which is the last nested component in the hierarchy. 最初,当页面被加载时,数据被传递下来,state.data中的所有内容都被用作指示符组件中的项目,这是层次结构中的最后一个嵌套组件。 So, my problem is that on state change the props are not updated from the steepcardcontainer component. 所以,我的问题是在状态改变时,道具不会从steepcardcontainer组件更新。 ie the state changes and I can see that, however the props aren't updated - in particular the VisualisatioContainer does not have its props updated, even though the state changed. 即状态改变,我可以看到,但道具没有更新 - 特别是VisualisatioContainer没有更新道具,即使状态改变。 That is the issue that I'm having. 这就是我所拥有的问题。 Any help is very much appreciated. 很感谢任何形式的帮助。

The first thing I would do is. 我要做的第一件事就是。

  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> ); } 

I believe you're mixing ES6 classes and React.createClass . 我相信你正在混合使用ES6类和React.createClass These are two different ways of creating React components. 这是创建React组件的两种不同方法。

See here: https://facebook.github.io/react/docs/reusable-components.html#es6-classes 请参见: https//facebook.github.io/react/docs/reusable-components.html#es6-classes

I'm kind of surprised this runs at all. 我对这次跑步感到很惊讶。 Here's the difference: 这是区别:

ES6 Class: ES6课程:

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

** **

React.createClass: React.createClass:

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

Fix that and see if the error persists. 修复此问题并查看错误是否仍然存在。

I just found what the problem was, I had shouldComponentUpdate() always returning false in visualisation component. 我刚刚发现了问题所在,我让shouldComponentUpdate()总是在可视化组件中返回false。 Hope this helps if anyone is having similar issue. 希望如果有人遇到类似问题,这会有所帮助。

暂无
暂无

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

相关问题 父组件在父状态更改时不必要地重新渲染子级 - Parent component unnecessarily re-rendering child on parent state change 反应子组件不会在更新的父状态上重新渲染 - React child component not re-rendering on updated parent state 父 React 组件不会在使用反应钩子的子项中更改 state 时重新渲染; - Parent React component not re-rendering on change state in child using react hooks; React子组件不会在状态更改时重新呈现 - React child component not re-rendering on state change React 组件不会在 state 更改时重新渲染 - React component not re-rendering on state change React 组件不会在 state 更改时重新呈现 - React component is not re-rendering on state change React:当只有子组件需要重新渲染时,如何防止父组件重新渲染 onmousemove 状态更改? - React: How can I prevent the parent component re-rendering onmousemove state change when only the child component needs re-rendering? onChange of <select>子组件中的元素未在React中重新呈现父状态,警告:未安装的组件 - onChange of <select> element in child component not re-rendering the parent state in React, Warning: unmounted component 从父组件firebase更改状态后,无法重新渲染组件 - react component not re-rendering on state change from parent component firebase 如果父级更新反应js中的上下文或状态,如何停止重新渲染子组件? - how to stop re-rendering of child component if parent update the context or state in react js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM