redux请求api的时候,传参应该在哪个生命周期?
发布于 5 年前 作者 vmto 3819 次浏览 来自 问答

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 回复

多看看文档… 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 已经被弃用了

componentDidMount

回到顶部