zoukankan      html  css  js  c++  java
  • 关于react 父级组件更新数据触发子组件更新渲染问题

    这个问题,困扰了我,特此记录。

    子组件显示父组件传来的props 做更新有 以下2种常用方式:

    1.直接使用

    class Child extends Component {
        render() {
            return <div>{this.props.someThings}</div>
        }
    }
    

    这种方式可以直接在子组件中使用,方便快捷,父组件的更新的数据直接影响子组件的值。

    2.转换成自己的state

        class Child extends Component {
          constructor(props) {
            super(props);
            this.state = {
              someThings: props.someThings
            };
          }
          componentWillReceiveProps(nextProps) {
            this.setState({someThings: nextProps.someThings});
          }
          render() {
            return <div>{this.state.someThings}</div>
          }
        }
    

    自己在构造函数中初始化一个值,在将 status 通过 componentWillReceiveProps 生命周期方法 更新

    自己喜欢用的是第二种,看起来易维护,代码结构清晰

    但是,我遇到一个问题,是在渲染第一次是正常的,之后怎么也不做渲染了

    后面发现了,是无意间使用了 提升react加载性能的 shouldComponentUpdate方法,该方法会阻止 states 的重新渲染

    无状态的函数声明组件,如何实现呢,正在解决中。。。

  • 相关阅读:
    gitbook
    Goland IDE使用
    go-zero RPC 框架安装 (goctl安装, protoc安装, etcd安装)
    go 打包部署
    GO redis
    go 常见异常
    go 异常处理
    go常用数据处理 (json, map, 结构体)
    Kafka日志消息
    【leetcode_easy_math】1317. Convert Integer to the Sum of Two No-Zero Integers
  • 原文地址:https://www.cnblogs.com/snowhite/p/15090019.html
Copyright © 2011-2022 走看看