简体   繁体   English

在条件渲染中组合的React组件

[英]React Components Combined on Conditional Render

I have two React components of the same class, which has it's own state storing data. 我有两个相同类的React组件,它们有自己的状态存储数据。 I am conditionally-rendering the components based on a prop of the parent (region) that is propagated by a dropdown change. 我有条件地根据通过下拉更改传播的父级(区域)的属性来渲染组件。 On the first dropdown change, the component is rendered with the title prop of the second component, but the data of the first component. 在第一个下拉更改中,使用第二个组件的标题prop渲染该组件,但是使用第一个组件的数据渲染该组件。 On the second dropdown change it works as intended. 在第二个下拉更改中,它按预期工作。

I have tried testing the components individually and they work fine with the region prop change. 我已经尝试过单独测试组件,并且它们可以与区域属性更改一起正常工作。 I also added a prop to one of the components and a conditional to prevent re-rendering on receiving props, but I got the same result. 我还向其中一个组件中添加了一个道具,并添加了一个有条件的条件以防止在接收道具时重新渲染,但是得到了相同的结果。 I could just hide one of the components depending on the region prop, but I would like to do this via conditional rendering. 我可以根据区域属性来隐藏其中一个组件,但是我想通过条件渲染来实现。

var content = null;
if(this.props.region === 'WEST'){
    content = (<Component title={'A'} region={this.props.region} dataSource={'someURL'} />);
}else{
    content = (<Component title={'B'} region={this.props.region} dataSource={'someOTHERURL'} />);
}

return (
    {content}
);

EDIT with more details: 编辑更多详细信息:

The component has a fetch call to the back end on both componentDidMount() and componentWillReceiveProps() that updates the component's state. 该组件在componentDidMount()和componentWillReceiveProps()上都对后端进行访存调用,以更新组件的状态。 Each instance of the component (title={'A'} and title={'B'}) works properly when rendered non-conditionally. 当无条件呈现时,组件的每个实例(title = {'A'}和title = {'B'})都可以正常工作。 Is it possible that the component that is initially rendered is trying to update its state at the same time that the new component is being rendered in its place? 最初渲染的组件是否有可能尝试在替换新组件的同时更新其状态?

fetch('/table?tablename=' + this.props.tableName + '&region=' + 
    nextProps.region + '&chartName=' + this.props.chartName, {
        method: "GET",
        headers: { "Accept": "application/json", "Content-Type": "application/json" }
        }).then(res => res.json())
        .then(data => this.setState({data: data}));
    }

If you're using shouldComponentUpdate or PureComponent this may be causing your issue. 如果您使用的是ShouldComponentUpdate或PureComponent,则可能是造成问题的原因。 It is hard to diagnose the problem without seeing more of the code. 如果不查看更多代码,就很难诊断问题。

It seems to me that the title prop in component is not getting updated on the first change because componentWillReceiveProps isn't getting triggered. 在我看来,由于未触发componentWillReceiveProps,组件的title属性在第一次更改时就没有更新。

I'd put this in a comment if there was room since it isn't an answer. 如果没有空间,我会在评论中注明,因为这不是答案。

You're unnecessarily mounting/unmounting components which is adding complexity with regard to the lifecycle methods. 您不必要地安装/卸载组件,这增加了生命周期方法的复杂性。 I'd always return a single component and just change the props. 我总是会返回一个组件,只是更改道具。 Continue to fetch in componentDidMount and componentWillReceiveProps but consider caching responses as well. 继续获取componentDidMountcomponentWillReceiveProps但也考虑缓存响应。

So maybe something like; 所以也许像

const data = {
  WEST: {
    title: 'A',
    url: 'someurl'
  },
  EAST: {
    title: 'B',
    url: 'anotherurl'
  },
}
const dataSource = data[this.props.region];

return <Component title={dataSource.title} region={this.props.region} url={dataSource.url} />

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

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