简体   繁体   English

React router 5,防止组件卸载

[英]React router 5, prevent component from unmounting

I'm building an app and run towards issue that I'm unable to fix -我正在构建一个应用程序并遇到我无法解决的问题 -

I'm fetching data from backend and listing it in a component.我正在从后端获取数据并将其列在组件中。 Component has select dropdown to filter the list.组件有 select 下拉列表来过滤列表。 Clicking on item I render the item details component using History and passing parameters with state, like that -单击项目,我使用 History 呈现项目详细信息组件并使用 state 传递参数,就像这样 -

onClick={() =>
          history.push({
            pathname: "/itemdetails",
            state: { item: item },
          })
        }

The problem is that when I go to items detail page, my item list component gets unmounted.问题是当我 go 到项目详细信息页面时,我的项目列表组件被卸载。 So when I go back to it like that (or by browser):所以当我 go 像这样(或通过浏览器)返回它时:

 onClick={() => history.goBack()}

item list component fetch data and render again while I want it to be in exact same state.项目列表组件获取数据并再次渲染,而我希望它与 state 完全相同。 Ideally remember scroll position, but at least with already rendered list and same selected dropdown.理想情况下,请记住滚动 position,但至少要使用已呈现的列表和相同的选定下拉列表。

Here were similar questions, but I didn't find solution for this particular problem.这里有类似的问题,但我没有找到解决这个特定问题的方法。 I've read somewhere that nested routes can do the trick, but I can't wrap my head around how to do it yet.我在某处读过嵌套路由可以解决问题,但我还不能解决如何做到这一点。

Router in App js looks like that(with omitted parts): App js中的路由器看起来像这样(省略了部分):

 <Router>
    <Sidebar />
    <Switch>
      <Route path="/home" component={HomeScreen} />
      <Redirect from="/" exact to="/home" />
      <Route path="/items" exact component={ItemsScreen} />
      <Route
        path="/itemsdetails"
        exact
        component={ItemDetailScreen}
      />
    </Switch>
  </Router>

I'm building an app and run towards issue that I'm unable to fix -我正在构建一个应用程序并遇到我无法解决的问题 -

I'm fetching data from backend and listing it in a component.我正在从后端获取数据并将其列在组件中。 Component has select dropdown to filter the list.组件有 select 下拉列表来过滤列表。 Clicking on item I render the item details component using History and passing parameters with state, like that -单击项目,我使用 History 呈现项目详细信息组件并使用 state 传递参数,就像这样 -

onClick={() =>
          history.push({
            pathname: "/itemdetails",
            state: { item: item },
          })
        }

The problem is that when I go to items detail page, my item list component gets unmounted.问题是当我 go 到项目详细信息页面时,我的项目列表组件被卸载。 So when I go back to it like that (or by browser):所以当我 go 像这样(或通过浏览器)返回它时:

 onClick={() => history.goBack()}

item list component fetch data and render again while I want it to be in exact same state.项目列表组件获取数据并再次渲染,而我希望它与 state 完全相同。 Ideally remember scroll position, but at least with already rendered list and same selected dropdown.理想情况下,请记住滚动 position,但至少要使用已呈现的列表和相同的选定下拉列表。

Here were similar questions, but I didn't find solution for this particular problem.这里有类似的问题,但我没有找到解决这个特定问题的方法。 I've read somewhere that nested routes can do the trick, but I can't wrap my head around how to do it yet.我在某处读过嵌套路由可以解决问题,但我还不能解决如何做到这一点。

Router in App js looks like that(with omitted parts): App js中的路由器看起来像这样(省略了部分):

 <Router>
    <Sidebar />
    <Switch>
      <Route path="/home" component={HomeScreen} />
      <Redirect from="/" exact to="/home" />
      <Route path="/items" exact component={ItemsScreen} />
      <Route
        path="/itemsdetails"
        exact
        component={ItemDetailScreen}
      />
    </Switch>
  </Router>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM