更新 React Native Stack Navigator 标头中的徽章

问题描述

I have added badges in react native stack navigator for cart and wish-list count. These counts must be fetched from API on every screen change or back event. I also want to update these count on add to cart and add to wish-list event from Product screen

谁能建议我怎么做? 任何参考代码文章都会非常有帮助。

提前致谢。

解决方法

如果需要从应用的任何屏幕全局更新 UI,那么我们可以实现全局状态管理。

有两种简单的方法来实现全局状态,当对它进行更改时更新 UI。

  1. React Redux
  2. 反应上下文 API

使用其中之一并在全局状态中进行更改,并将值分配给需要更改 UI 的 UI 组件。

您只需调用 API 并更新全局状态即可更新徽章计数。

查看官方文档以实现全局状态: