在componentDidMount,原因如下:
- componentDidMount指的是第一次插入dom完畢,無論在同步和異步模式下都僅會觸發一次
- 在目前16.3之前的react版本中 ,react是同步渲染的,在componentWillMount中接口調用,有可能不會觸發界面渲染,而在componentDidMount中渲染一定會觸發界面渲染,具體可以看這個issue
- 在16.3之后react開始異步渲染,在異步渲染模式下,使用componentWillMount會被多次調用,並且存在內存泄漏等問題
- 關於在componentWillMount比componentDidMount請求早,界面渲染會更快等說法,在最新的react blog里也解釋了,其實這一說法一直來並不成立; 具體是componentWillMount會立即執行,執行完之后會立即進行render