【问题标题】:componentWillMount vs componentDidMount?componentWillMount 与 componentDidMount?
【发布时间】:2020-10-09 11:05:26
【问题描述】:

既然在新版本的 React Native 中,componentWillMount 已经被贬值了,那么 componentWillMount 和 componentDidMount 的作用是一样的吗? 在我的研究中,componentWillMount 在第一次渲染之前执行,componentDidMount 在第一次渲染之后执行,现在的问题是在 react native 中是否有任何组件在第一次渲染之前自动执行?

【问题讨论】:

标签: reactjs react-native


【解决方案1】:

您可以在第一次渲染之前使用constructor() 做一些事情

UNSAFE_componentWillMount() 在挂载之前被调用。它在 render() 之前调用,因此在该方法中同步调用 setState() 不会触发额外的渲染。一般来说,我们建议使用 constructor() 来初始化状态。

Taken from the official docs

【讨论】:

    猜你喜欢
    • 2018-04-13
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    相关资源
    最近更新 更多