CNode

redux请求api的时候,传参应该在哪个生命周期?

问答
Vvmto发布于7 年前最后回复7 年前2 回复4438 浏览0 收藏

redux请求api的时候,传参应该在哪个生命周期?

//code...
  getTopics(tab) {
    this.props.dispatch((dispatch, getState) => {
      axios.get(`/topics?tab=${tab}&page=1&limit=10`) .then(res => {
            dispatch({
              type: "Topics_Succ",
              data: res.data
            });
        })
    });
  }

  componentDidMount() {
    this.getTopics('good');
    this.getTopics('ask');
  }
  // render...

大家都是怎么用?

componentDidMount componentWillReceiveProps

查看回复

回复 (2)

E
ekoneko#1·7 年前

多看看文档... https://reactjs.org/docs/react-component.html#componentdidmount

If you need to load data from a remote endpoint, this is a good place to instantiate the network request.

componentWillReceiveProps 已经被弃用了

W
wjjhhh#2·7 年前

componentDidMount

参与回复
登录后即可参与回复。登录