javascript - 防止在 react 中更改状态时呈现子组件

标签 javascript reactjs

我有一个 React 的父组件和子组件。在这里,我将 id 作为 Prop 从 parent 传递给 child ,我正在保存使用状态输入的文本区域的值。每当我在文本区域中输入时。子组件得到更新。如何防止子组件更新文本区域中输入的每个值?请帮助我。

class Child extends React.Component {
  constructor() {
    super();
  }
  componentWillMount(){
    console.log('child component Will '+this.props.id);
  }
  componentDidMount(){
    console.log('child component Did '+this.props.id);
  }
  render() {
    console.log('child render '+this.props.id);
    return <p>Child {this.props.id}</p>;
  }
}
class Application extends React.Component {
  constructor(){
    super();
    this.state={
      id:1,
      textValue:undefined
    }
  }
  componentWillMount(){
    console.log('parent component Will');
  }
  componentDidMount(){
    console.log('parent component Did');
  }
  render() {
    console.log('parent render');
    return <div>
      <textarea onChange={(event)=>{
         this.setState(
           {textValue:(event.target.value)})
        }
          }></textarea>
      <Child id='1'/>
      <Child id='2'/>
    </div>;
  }
}

ReactDOM.render(<Application />, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.js"></script>
<div id="app"></div>

Code pen Link

最佳答案

您可以使用 React.PureComponent 而不是扩展 React.Component。两者之间的区别在于,后者还对每个渲染之间的 propsstate 进行了比较;如果什么都没有改变,它就不会更新。

official documentation上也推荐这个:

If your React component's render() function renders the same result given the same props and state, you can use React.PureComponent for a performance boost in some cases.


看看下面的代码。我只更改了第一行代码以扩展正确的类。

class Child extends React.PureComponent {
  constructor() {
    super();
  }
  componentWillMount(){
    console.log('child component Will '+this.props.id);
  }
  componentDidMount(){
    console.log('child component Did '+this.props.id);
  }
  render() {
    console.log('child render '+this.props.id);
    return <p>Child {this.props.id}</p>;
  }
}
class Application extends React.Component {
  constructor(){
    super();
    this.state={
      id:1,
      textValue:undefined
    }
  }
  componentWillMount(){
    console.log('parent component Will');
  }
  componentDidMount(){
    console.log('parent component Did');
  }
  render() {
    console.log('parent render');
    return <div>
      <textarea onChange={(event)=>{
         this.setState(
           {textValue:(event.target.value)})
        }
          }></textarea>
      <Child id='1'/>
      <Child id='2'/>
    </div>;
  }
}

ReactDOM.render(<Application />, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.js"></script>
<div id="app"></div>

编辑:我更新了您的问题并使您的代码可以在代码片段中运行,以便它可以与我的片段进行比较。

关于javascript - 防止在 react 中更改状态时呈现子组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45800175/

相关文章:

javascript - 在 ajax 调用 Promise 中的 .done() 函数之前使用回调函数

reactjs - 从 react 导航堆栈中删除最后一条路线

javascript - 如何在 react 中获取对象中的嵌套数组?

javascript - 鼠标弹起与点击

javascript - 在单页应用程序(SPA)中处理身份验证的最佳方法

javascript - Material UI 自动完成,redux-form 值被破坏

unit-testing - 使用Sinon、Mocha、Enzyme 和 React 模拟窗口

javascript - react : store array objects in state and display with keys

javascript - 当我尝试将鼠标悬停在其关闭的第二个子元素上时 - Jquery 或 css 问题?

javascript - d3.js 的饼图转换图中的输入数据不显示标签名称