【发布时间】:2018-07-30 07:01:59
【问题描述】:
在 ReactJs 中,我们可以使用 componentWillMount 来做什么,而我们不能通过构造函数来做到这一点?两者都在组件渲染之前调用一次。
import React from 'react';
class Display extends React.Component {
constructor(props)
{
super(props)
console.log('Display.Constructor...')
console.log(this.props)
}
componentWillMount(){
console.log('Display.componentWillMount...')
console.log(this.props)
}
【问题讨论】:
-
您没有在代码中使用 componentWillUpdate。
-
@kojow7 谢谢。修正了错字。
-
reactjs.org/docs/react-component.html#componentwillmount "一般情况下,我们建议改用
constructor()
。" -
您应该查看 React 生命周期方法:engineering.musefind.com/… 和 medium.com/@baphemot/… 都可能是您阅读的有用页面。
-
@fahmi,您是否检查过以下内容:*.com/questions/40828004/…
标签: javascript reactjs react-native