2017-08-14 138 views
-3

在反应的次序呈现的,假设我有丙名称输入分量= A,B,C. 它们在顺序呈现反应。组件未在状态改变

render() { 
    return(
     <Input name="A" /> 
     <Input name="B" /> 
     <Input name="C" /> 
    ); 
} 

然后我的状态改变ç的顺序第一ç然后A. 组分Ç重新呈现的顺序第一然后Ç。它们没有在状态变化的顺序呈现(ç然后

参见下面给出的代码段。 我发现了把其形式为C

设定状态 B的

设定状态

设定状态渲染的

渲染B的

C的渲染

class Input extends React.Component { 
 
    
 
    
 
\t componentWillMount() { 
 
\t \t \t this.props.addInput(this); 
 
\t } 
 
    
 
    state = { 
 
    \t error: false 
 
    } 
 
    
 
    check() { 
 
    console.log("set state of", this.props.name) 
 
    this.setState({ 
 
     error: true 
 
    }) 
 
    } 
 

 
    render() { 
 
    \t console.log("Render of", this.props.name) 
 
    return (
 
     <input /> 
 
    ); 
 
    } 
 
} 
 
class Hello extends React.Component { 
 
    
 
    constructor(props) { 
 
    super(props); 
 
    this.inputs = {}; 
 
} 
 

 
    addInput(component) { 
 
     this.inputs[component.props.name] = component; 
 
     console.log(this.inputs); 
 
    } 
 
    
 
    checkAll() { 
 
    const inputs = this.inputs; 
 
    Object.keys(inputs).reverse().forEach(name => { 
 
    inputs[name].check() 
 
    }); 
 
    } 
 
    
 
    render() { 
 
    return (
 
    \t <div> 
 
     <Input addInput={(c) => this.addInput(c)} name="A"/> 
 
     <Input addInput={(c) => this.addInput(c)} name="B"/> 
 
     <Input addInput={(c) => this.addInput(c)} name="C"/> 
 
     <button onClick={() => this.checkAll()}>click here</button> 
 
     </div> 
 
    ); 
 
    } 
 
} 
 

 
ReactDOM.render(
 
    <Hello initialName="World"/>, 
 
    document.getElementById('container') 
 
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.js"></script> 
 
<div id="container"> 
 
    <!-- This element's contents will be replaced with your component. --> 
 
</div>

+2

您必须包含相关的代码部分才能重现问题。 –

+0

嗨,我只是想知道反应组件渲染的顺序。 – PranavPinarayi

+0

@YuryTarabanko添加代码片段请检查 – PranavPinarayi

回答

1

这是JSX应该如何默认工作。

如果你想呈现在最后状态变化的订单成分,你必须把所有的零件,它们放进一个数组或有一个集合的componentName: componentInstance,还具有componentName: lastUpdated集合[或数组(或数组项目形式{ componentName: string, lastUpdated: Date }),您可以在其中修改每个组件的lastUpdated值,然后按日期值对componentName: componentInstance集合或数组进行排序。

然后在JSX中只有.map

+0

感谢您的回答。你能否请检查更新的代码。 – PranavPinarayi