简体   繁体   English

在Redux状态不变的情况下重新访问时如何在页面路由组件中进行Redux更新?

[英]How to make Redux update in a page-route component when revisiting it while Redux state doesn't change?

I only have my authentication in Redux to make it easier to get authentication and user data throughout the app, except for one page I decided to dispatch fetch some data from mongoDB with Redux (in a Home page). 我只在Redux中进行身份验证,以便更轻松地在整个应用程序中获取身份验证和用户数据,除了我决定通过Redux分派从mongoDB获取某些数据的页面(在主页中)。

When I go to another page, I make some modifications to the database. 当我转到另一个页面时,我对数据库进行了一些修改。 But when I come back to the Home page, it doesn't go fetch the data again, because nothing changed in it's state or props. 但是,当我返回首页时,它不会再次获取数据,因为状态或道具没有任何变化。 Redux has it's own 'DidUpdate' so I can't force an update in the default React component lifecycle... Redux有它自己的'DidUpdate',所以我不能在默认的React组件生命周期中强制更新...

Is there a way to force the Home page (which uses Redux) to fetch DB data again when I go to that route (so that I can see the newly added DB data)? 当我转到该路由时(是否可以看到新添加的DB数据),是否有一种方法可以强制主页(使用Redux)再次获取DB数据?

Or am I stuck having to a) modify the Redux state from another page b) modify the React state of the parent of the Home component-page to trigger am update and fetch c) run my whole app through Redux so that any changes I make to the Redux store will trigger an update and call the fetching of data from the DB? 还是我必须坚持a)从另一个页面修改Redux状态b)修改Home组件页面的父对象的React状态以触发am更新并获取c)通过Redux运行我的整个应用程序,以便我进行任何更改到Redux存储库将触发更新并调用从DB提取数据?

Thank you for any insights you can offer ;) 感谢您提供的任何见解;)

EDIT: Sorry, all good. 编辑:对不起,一切都很好。 I had a 'flawed' algorithm that checked if a recent fetch was done and wouldn't do another. 我有一个“有缺陷的”算法,该算法检查最近的提取是否已完成,并且不会再次执行。 All is ok now. 现在一切都好。 Sorry for the bother. 对不起,麻烦了。

So whenever you visit any page in React application, as per the life-cycle Componentdidmount() this function will get a trigger. 因此,每当您访问React应用程序中的任何页面时,根据生命周期Componentdidmount()此函数都会触发。 So if you want to fetch new data from your API the just call you API URL of action Componentdidmount() . 因此,如果您想从API中获取新数据,只需调用操作Componentdidmount() API URL。

  componentDidMount() {
    if (this.props.location) {// you can check your location as well.
      // call your API or function which will call your API
    }
  }

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

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