简体   繁体   English

当另一个组件的 state 发生变化时如何重新加载组件 React

[英]How to reload the component when the state of another component changes React

I am making an e-commerce app with react and firebase the <cart /> component contains two child components我正在制作一个带有 react 和 firebase 的电子商务应用程序<cart />组件包含两个子组件

1- <BasketItem /> (this component will be duplicated as many Items as the user adds to the cart and it has the functionality to let user modify the quantity of the Item ) 1- <BasketItem /> (该组件将复制用户添加到购物车中的尽可能多的项目,并且它具有让用户修改项目数量的功能)

2- <OrderSummary /> (this component will show the user the the total price of his cart and the shipping fees) 2- <OrderSummary /> (这个组件将向用户显示他的购物车的总价格和运费)

of course all the data of the items and the total will be gotten from the database (I am using firestore) but but the issue is when the user update the quantity of the items from the <BasketItem /> component the total price from the <OrderSummary /> did not get updated as well because the database call will be done already and no way to recall the function that gets the data from the database because there is no parent- child relation between the two components and I can't pass props between them I have tried to use react-context-api for this but that did not solve the issue because the up to date is on the database and the only way to update the total value is to refresh the page and this is not a good user experience to have.当然,项目的所有数据和总数将从数据库中获取(我正在使用 firestore),但问题是当用户从<BasketItem />组件更新项目的数量时,从<OrderSummary />也没有得到更新,因为数据库调用已经完成并且无法调用从数据库获取数据的 function,因为两个组件之间没有父子关系并且我无法传递props在他们之间,我尝试为此使用 react-context-api 但这并没有解决问题,因为数据库上是最新的,更新总值的唯一方法是刷新页面,这不是一个好的用户体验。

In any React all, all affected UI components should be updated when you update the cart information in the state (as they should all be observing that).在任何 React all 中,当您更新 state 中的购物车信息时,所有受影响的 UI 组件都应该更新(因为他们都应该注意这一点)。

When using Firestore, you'll use a so-called CQRS pattern, where the updates sent to the database, are a separate stream from the listeners to the database.使用 Firestore 时,您将使用所谓的 CQRS 模式,其中发送数据库的更新是从侦听器到数据库的单独 stream。 You'll typically add listeners when you create/mount the components, so that they're always listening while the component is visible to the user.您通常会在创建/安装组件时添加侦听器,以便在组件对用户可见时它们始终在侦听。

With that setup in place, the update flow becomes:有了该设置,更新流程变为:

  1. Your code write a new value to the database through the Firebase SDK.您的代码通过 Firebase SDK 将新值写入数据库。
  2. This gets written to the database on the server at some point.这会在某个时候写入服务器上的数据库。
  3. The servers sends this information to all other clients, and a confirmation to the client who wrote the value.服务器将此信息发送给所有其他客户端,并向写入该值的客户端确认。
  4. This triggers the listener that you registered before.这会触发您之前注册的侦听器。
  5. The listener updates the data in the state of the app.监听器更新应用程序的 state 中的数据。
  6. And that then finally triggers a repaint of the affected components.然后最终触发受影响组件的重绘。

This is the general flow, but it applies to all cases I can think of.这是一般流程,但它适用于我能想到的所有情况。

Usually the best practice is to have the state stored on the front-end too in the form of context-api or redux-store or simply as component's state, they try syncing this state on the front-end with DB when user triggered submit/order action.通常最好的做法是将 state 也以 context-api 或 redux-store 的形式存储在前端,或者简单地作为组件的 state,他们尝试在用户触发时将此 Z9ED39E2EA931586B73/前端-submitEZEF 与 DB 同步命令动作。

If you had to update your DB on every quantity change, then you have to update the same on front-end state/redux-store too.如果您必须在每次数量变化时更新您的数据库,那么您也必须在前端状态/redux-store 上更新相同的数据库。

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

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