路由改变时强刷数据

2018-09-04  本文已影响12人  Ysj1111

componentWillReceiveProps,利用该生命周期,进行重新render的预处理操作

class NewsList extends Component {
  componentDidMount () {
     this.fetchData(this.props.location);
  }

  fetchData(location) {
    const type = location.pathname.replace('/', '') || 'top'
    this.props.dispatch(fetchListData(type))
  }

  componentWillReceiveProps(nextProps) {
     if (nextProps.location.pathname != this.props.location.pathname) {
         this.fetchData(nextProps.location);
     } 
  }

  render () {
    ...
  }
}

参考链接

上一篇 下一篇

猜你喜欢

热点阅读