React 如何监听路由变化重新渲染组件

那我们所需要做的只是: 当路由改变时,根据路由,也去请求一下数据就OK了,于是乎:

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 () {
    ...
  }
}

猜你喜欢

转载自blog.csdn.net/u010565037/article/details/89015664