简体   繁体   English

更改 url 参数时,React 路由器不会重新加载组件

[英]React router not reloading Component when changing url params

I know that it's not a default behaviour / feature of react-router to help us reload easily the current component but I really need this in my application.我知道它不是 react-router 的默认行为/功能来帮助我们轻松重新加载当前组件,但我真的需要在我的应用程序中使用它。

My application deals with products.我的应用程序处理产品。 I have a product list that I can load, and when I click on an item, it displays the concerned product details.我有一个可以加载的产品列表,当我点击一个项目时,它会显示相关的产品详细信息。

On that page, I have related product links that load the same component, but with another product details, located at在该页面上,我有加载相同组件的相关产品链接,但还有另一个产品详细信息,位于

<Route path="/products/:id/details" component={ProductDetail} />

I m fetching data in my componentWillMount, and it seems that if I only change the URL, a new component is NOT mounted, and so, I m always having my old data displayed, without fetching anything.我正在我的 componentWillMount 中获取数据,似乎如果我只更改 URL,则不会安装新组件,因此,我总是显示我的旧数据,而不获取任何内容。

As a beginner using React, I'm looking for some help, or some tricks to reload the component concerned by that page.作为使用 React 的初学者,我正在寻找一些帮助或技巧来重新加载该页面相关的组件。 I mean being able to reload the ProductDetail with the good product.我的意思是能够用好的产品重新加载 ProductDetail。

I tried to look around with componentWillUpdate (a method in which I can see that the router URI changes :D) but I can't setState inside of it to make my component reload (it doesn't seem to be a good practice at all)我尝试使用 componentWillUpdate 环顾四周(一种可以看到路由器 URI 更改的方法:D),但我无法在其中设置 setState 来重新加载我的组件(这似乎根本不是一个好习惯) )

Any idea how can I make this work ?知道我怎样才能做到这一点吗?

EDIT : According to the first answer, I have to use onEnter.编辑:根据第一个答案,我必须使用 onEnter。 I m now stuck with the way of passing state/props to the concerned component :我现在坚持将状态/道具传递给相关组件的方式:

const onEnterMethod = () => {
    return fetch(URL)
        .then(res => res.json())
        .then(cmp => {
            if (cmp.length === 1) {
             // How to pass state / props to the next component ?
            }
        });
};

The way to handle it depends if you are using flux, redux or however you want to manage your actions.处理它的方式取决于您使用的是flux、redux 还是您想管理您的操作。 On top of it I would try to make use of onChange property of Route component (check React router docs ):最重要的是,我会尝试使用 Route 组件的 onChange 属性(检查React 路由器文档):

    <Route path="/products/:id/details" component={ProductDetail} onChange={someMethod} />

And then in the someMethod create the action if you are using redux or however is done in flux.然后在 someMethod 中创建操作,如果您使用的是 redux 或者是在flux中完成的。 The redux would be: Redux 将是:

    <Route path="/products/:id/details" component={ProductDetail} onEnter={onEnterHandler(store)} />

And the onEnterHandler with the redux store:以及带有 redux 存储的 onEnterHandler:

    function onEnterHandler(store) {
      return (nextState, replace) => {
        store.dispatch({
          type: "CHANGEPRODUCT",
          payload: nextState.params.id
        })
      };
    }

And then in your ProductDetail component you would print the new information (It would require a bit more of learning in redux and redux-sagas libraries to complete that part).然后在您的 ProductDetail 组件中,您将打印新信息(需要在 redux 和 redux-saga 库中进行更多学习才能完成该部分)。

Keep in mind that React is just the view part, trying to solve all those problems using only react is not only not recommended but also would mess up your code.请记住,React 只是视图部分,尝试仅使用 React 来解决所有这些问题不仅不推荐,而且还会弄乱您的代码。

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

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