【问题标题】:Ionic React props.match.params doesn't updateIonic React props.match.params 不更新
【发布时间】:2021-03-19 19:16:44
【问题描述】:

我正在使用 React 和 Ionic 构建一个展示房地产的网站。

我的 App.tsx

中有此路由
<IonApp>
  <IonReactRouter>
    <Nav />
    <IonRouterOutlet id="first">
      <Route exact path="/" component={Home} />
      <Route exact path="/all-properties" component={Properties} />
      <Route exact path="/property/:id" component={Property} />
    </IonRouterOutlet>
  </IonReactRouter>
</IonApp>

我链接到我网站中的单个 "/property/:id" 页面,如下所示:

<Link to={"/property/" + variableId}> ... </Link>

现在在我的"/property/:id" 页面上,我可以像这样从 URL 访问 id:

useIonViewDidEnter(() => {
  console.log(props.match.params.id); // This works the first time
});

问题是,当我点击返回主屏幕,然后访问我的另一个 "/property/:id" 页面时,props.match.params.id 保持不变。没有更新。

可以做些什么来解决它?

【问题讨论】:

  • 一些关于如何访问路线的代码会有所帮助
  • @AaronSaunders 我不确定你的意思,你能扩展一下吗?

标签: reactjs ionic-framework react-router-dom ionic-react


【解决方案1】:

原来问题出在 Ionics 生命周期方法。

我的解决方案是像这样创建一个 useEffect:

useEffect(() => {
   console.log(props.match.params.id)
}, [props.match.params.id])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多