[英]Why doesn't this child component rerender?
I'm experimenting with ReactJS and I'm trying to understand how child component rendering is triggered. 我正在使用ReactJS进行实验,并且试图了解如何触发子组件渲染。 In ReactJS, if I set up an example like this:
在ReactJS中,如果我设置这样的示例:
var externalCounterVar = 10
class Counter extends React.Component {
constructor(props){
super(props);
this.state = props;
}
render() {
console.log('rendering counter')
return (
<div> {externalCounterVar} </div>
)
}
}
class Main extends React.Component {
constructor(props){
super(props);
}
handleClick() {
externalCounterVar += 1;
}
rerender(){
this.render();
}
render() {
console.log('rendering');
return (
<div>
<button onClick={this.rerender.bind(this)} />
<Counter counter={externalCounterVar} />
</div>
)
}
}
ReactDOM.render(<Main />, document.getElementById('root'));
I'm not sure I understand why when you "rerender" it calls the render method of Main but not Counter? 我不确定我为什么会在您“渲染”时调用Main而不是Counter的render方法? It seems like it should call both render methods since it's rendering Main and Counter is a child of Main.
似乎应该同时调用这两个render方法,因为它渲染的是Main,而Counter是Main的子级。
So when rerender is called, 'rendering' will print but 'rendering counter' will not. 因此,当调用rerender时,将打印“ rendering”,但不会打印“ rendering counter”。
It looks like you're overlooking one of the main benefits of using React, namely how state works. 看起来您正在忽略使用React的主要好处之一,即状态如何工作。
this.render
within a React component this.render
this.state = ...
this.state = ...
this.setState
to set your state. this.setState
设置状态。 Rewritten, your code should look something like the following: 重写后,您的代码应类似于以下内容:
const externalCounterVar = 10
class Counter extends React.Component {
render() {
console.log('rendering counter')
return (
<div> {this.props.counter} </div>
)
}
}
class Main extends React.Component {
state = {
counter: externalCounterVar
}
handleClick() {
this.setState({counter: this.state.counter + 1});
}
render() {
console.log('rendering');
return (
<div>
<button onClick={this.handleClick.bind(this)} />
<Counter counter={this.state.counter} />
</div>
)
}
}
By calling this.setState
, React automatically knows it needs to rerender your component, and as a result, all child components will also be rerendered. 通过调用
this.setState
,React自动知道它需要重新渲染您的组件,因此,所有子组件也将被重新渲染。
Hope this helps! 希望这可以帮助!
In this case you don't have to use rerender
method, also with purpose re-render all child components you need update state with method setState
. 在这种情况下,您不必使用
rerender
方法,也可以使用setState
方法重新渲染所有需要更新状态的子组件。 And also accordingly to this you have to "move state up". 并且也相应地对这个你要“动起来的状态”。
Here my example: 这是我的例子:
class Counter extends React.Component { render() { console.log('rendering counter'); return (<div> {this.props.counter} </div>); } } class Main extends React.Component { constructor(props){ super(props); this.state = {counter: props.counter}; this.handleClick = this.handleClick.bind(this); } handleClick() { this.setState(prevState => ({counter: ++prevState.counter})); } render() { console.log('rendering'); return ( <div> <button onClick={this.handleClick} /> <Counter counter={this.state.counter} /> </div> ); } } var externalCounterVar = 10; ReactDOM.render( <Main counter={externalCounterVar} />, document.getElementById('root') );
In some situations you can use this.forceUpdate()
to call re-render. 在某些情况下,您可以使用
this.forceUpdate()
调用重新渲染。 But, if you can not do this, do not do. 但是,如果您不能执行此操作,请不要执行。 https://facebook.github.io/react/docs/react-component.html#forceupdate
https://facebook.github.io/react/docs/react-component.html#forceupdate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.