• React componentWillMount


    今天在写React时,遇到的一个问题:

    我在父组件里引用了子组件,在 componentWillMount() 中调用了一个异步函数,在返回结果中调用 this.setState() 来保存数据,在子组件的select下拉框里,使用state来渲染,结果子组件一直没值。

    仔细检查后,查寻API:

    omponentWillMount 在渲染前调用,在客户端也在服务端。

    componentDidMount : 在第一次渲染后调用,只在客户端。之后组件已经生成了对应的DOM结构,可以通过this.getDOMNode()来进行访问,在这方法中调用setState方法,会触发重渲染。

     componentWillReceiveProps 在组件接收到一个新的 prop (更新后)时被调用。这个方法在初始化render时不会被调用。

    shouldComponentUpdate 返回一个布尔值。在组件接收到新的props或者state时被调用。在初始化时或者使用forceUpdate时不被调用。 可以在你确认不需要更新组件时使用。

    componentWillUpdate在组件接收到新的props或者state但还没有render时被调用。在初始化时不会被调用。

    componentDidUpdate 在组件完成更新后立即调用。在初始化时不会被调用。 componentWillUnmount在组件从 DOM 中移除的时候立刻被调用。

    解决办法:

    将异步方法这些放在componentDidMount中,就不会出现这个问题

  • 相关阅读:
    linux 命令展示该目录下的所有子目录及文件结构 tree
    ssh-keygen
    centos安装git
    spring-boot parent变更为依赖方式
    centos docker compose安装
    UML——活动图
    UML——构件图
    UML——部署图
    UML——用例图
    UML——关系
  • 原文地址:https://www.cnblogs.com/tylz/p/11224849.html
Copyright © 2020-2023  润新知