亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

React componentDidUpdate 在 url 更改時不會更改

React componentDidUpdate 在 url 更改時不會更改

九州編程 2021-06-30 09:49:49
Match.params 沒有在componentDidUpdate().我的路線是/sidebar/:id,如果從sidebar/1我去sidebar/2的this.props.match.params.id是仍然顯示id為1,而不是更新的URLid 中2componentDidUpdate()嘗試過props.location但仍然顯示舊網址的數據。側邊欄頁面組件 componentDidMount () {    const id  = this.props.match.params.id;    //fetching some data}componentDidUpdate(prevProps) {    console.log("Prevprops",prevProps.match.params.id);    console.log("Cuurent id",this.props.match.params.id);    //if both ids not same then fetch data again}路由器const routes = [  {    path: "sidebar/:id",    component: asyncComponent(() => import('../sidebarPage')),  },];class AppRouter extends Component {  render() {    const { url } = this.props; return (      <div>        {routes.map(singleRoute => {          const { path, exact, ...otherProps } = singleRoute;          return (            <Route              exact={exact === false ? false : true}              key={singleRoute.path}              path={`${url}/${singleRoute.path}`}           />          );        })}      </div>    );  }}我希望當我去sidebar/1到sidebar/2的componentDidUpdate this.props.match.params.id節目2和prevProps表演1。
查看完整描述

1 回答

?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

您asyncComponent僅使用初始值props并且不會對更改做出反應。


試試這個代碼:


async componentDidMount() {

  this.mounted = true;

  const { default: Component } = await importComponent();

  Nprogress.done();

  if (this.mounted) {

    this.setState({

      component: Component

    });

  }

}


render() {

  const Component = this.state.component;

  return (

    <ReactPlaceholder type="text" rows={7} ready={Component !== null}>

      {Component ? <Component {...this.props}/> : <div/>}

    </ReactPlaceholder>

  );

}


查看完整回答
反對 回復 2021-07-01
  • 1 回答
  • 0 關注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號