简体   繁体   English

为什么不重新渲染此子组件?

[英]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的主要好处之一,即状态如何工作。

  1. You never, ever need to call this.render within a React component 您永远都不需要在React组件中调用this.render
  2. You should never set state dynamically, ie: this.state = ... 您永远不要动态设置状态,即: this.state = ...
  3. You should always use 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.

相关问题 子组件不会重新渲染,但父组件会重新渲染。 如何让子组件重新渲染? - Child component doesn't rerender but parent component does rerender. How to make child component rerender? React - 更改 key prop 不会导致子组件的重新渲染 - React - changing key prop doesn't cause rerender of child component React 组件不会使用相同的道具重新渲染,但子状态会发生变化 - React component doesn't rerender with the same props but child state changes 母组件控制子组件的输入,但不会在更改时重新呈现 - Mother component controlling input for child components, but doesn't rerender on change 为什么 state 上的 React Native 组件不重新渲染 - Why doesn't React Native component rerender on state change 家长重新提交不会更新子道具 - Parent rerender doesn't update child props ReactJS:组件不会在状态更改时重新呈现 - ReactJS: Component doesn't rerender on state change react.js为什么更改道具后组件不会立即重新渲染 - react.js why component doesn't rerender immediately after changing props ReactJS,它不会在状态改变时重新渲染子组件 - ReactJS, it won't rerender child component on state change 试剂不会在let内部使用deref-rerender组件 - Reagent doesn't rerender component with deref-ing inside of let
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM