在读的一本叫《 The Road to learn React 》的书里面,API 调用是放在 render 之后的 componentDidMount 里的,但是刚才在油管上看的一个视频里面是放在 render 之前的 componentWillMount 里的,两种方法好像都能完成调用,这只是习惯问题,还是存在孰好孰坏之分?
1
fengbjhqs 2019 年 4 月 24 日
componentWillMount 17 版以后可能就没有了,
|
2
paullee 2019 年 4 月 24 日
|
3
triplecheese OP @fengbjhqs 好像是的,看来还是用 componentDidMount 比较好一点
|
4
triplecheese OP @paullee 了解了,谢谢
|
5
Sapp 2019 年 4 月 24 日
componentWillMount 不适合做异步
不过现在不都是 hooks 了吗? |
6
triplecheese OP @Sapp 还没学到 Hooks🤣
|
7
Sapp 2019 年 4 月 24 日
@triplecheese 如果不是为了面试,其实那些 api 不学也行了...
|
8
ChefIsAwesome 2019 年 4 月 24 日
你把 willmount 当作 constructor 就行了。
|
9
triplecheese OP @ChefIsAwesome 嗯嗯,有道理
|
10
zqx 2019 年 4 月 24 日 via Android
构造函数和 didmount 的执行时间可能也就差几微秒,这两个都可以吧?
|
11
duzhihao 2019 年 4 月 24 日 via iPhone
我记得官方文档建议放在 componentDidMount 里面
|
12
CocaColf 2019 年 4 月 24 日
componentDidMount 里面
|
13
MichaelJack 2019 年 4 月 24 日
componentWillMount 已弃用 现在只能在 componentDidMount
|