Send data between tabs in React.
In this article will look at how to send data between components in React globally, even in different browser tabs.
Story
Imagine you have a list of items, such as users.
Each of the users can be opened in a modal window for modification.
You don't have any subscriptions to the backend, which means that the list of users won't automatically synchronize with the backend if any user changes.
So, once a user's profile is updated, you want to automatically refresh the list of users under the modal window (even in all other tabs of your website).
What will we do to synchronize data in these unrelated components and tabs?
Solution
The modal window and the list of users should be able to exchange events and data.
Thus, if an action is performed in the modal window, the event should be sent to all components waiting for this kind of action (e.g. the list of users), so that they can react to this event, for example, by synchronizing data.
Let's set up such communication between the "UserList" and "UserProfileModal" components by using a small package use-app-events:
const UserProfileModal = () => { // retrieve a user ID from URL, for example const { userId } = useParams(); // 1. Create an instance of useAppEvents const { notifyEventListeners } = useAppEvents(); const submitUpdate = async () => { // send a request to BE here, await the response... // 2. Send an event containing the updated user ID to // all other components that are listening for it notifyEventListeners('user-update', userId); }; return <button onClick={submitUpdate}>Save changes</button>; };
? Modal window
? The list of users
const UserList = () => { const [users, setUsers] = useState([]); // 1. Create an instance of useAppEvents const { listenForEvents } = useAppEvents(); // 2. Listen and wait for the 'user-update' event to happen in the app listenForEvents('user-update', (userId) => { // 3. React to the occurred event by loading the refreshed // list of users from BE here... }); return users.map((user) => ( // render users here... )); };
use-app-events is a small open-source package with no dependencies and risks, it is also actively maintained and safe to use.
At this point, the update of the user profile in UserProfileModal will automatically notify all listeners like UserList, which will trigger a refresh of the list of users in UserList, resulting in a better UX.
It doesn't matter where UserList and UserProfileModal are placed in the component tree, they will still be able to send data between each other, even in different browser tabs.
Conclusion
If you need to effortlessly set up global communication to exchange data between components - make use of the use-app-events package.
It provides an easy-to-use API, extensive documentation, and strict typing to ensure you have the best developer experience.
以上是Send data between tabs in React.的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

JavaScript是现代Web开发的基石,它的主要功能包括事件驱动编程、动态内容生成和异步编程。1)事件驱动编程允许网页根据用户操作动态变化。2)动态内容生成使得页面内容可以根据条件调整。3)异步编程确保用户界面不被阻塞。JavaScript广泛应用于网页交互、单页面应用和服务器端开发,极大地提升了用户体验和跨平台开发的灵活性。

Python和JavaScript开发者的薪资没有绝对的高低,具体取决于技能和行业需求。1.Python在数据科学和机器学习领域可能薪资更高。2.JavaScript在前端和全栈开发中需求大,薪资也可观。3.影响因素包括经验、地理位置、公司规模和特定技能。

实现视差滚动和元素动画效果的探讨本文将探讨如何实现类似资生堂官网(https://www.shiseido.co.jp/sb/wonderland/)中�...

JavaScript的最新趋势包括TypeScript的崛起、现代框架和库的流行以及WebAssembly的应用。未来前景涵盖更强大的类型系统、服务器端JavaScript的发展、人工智能和机器学习的扩展以及物联网和边缘计算的潜力。

如何在JavaScript中将具有相同ID的数组元素合并到一个对象中?在处理数据时,我们常常会遇到需要将具有相同ID�...

不同JavaScript引擎在解析和执行JavaScript代码时,效果会有所不同,因为每个引擎的实现原理和优化策略各有差异。1.词法分析:将源码转换为词法单元。2.语法分析:生成抽象语法树。3.优化和编译:通过JIT编译器生成机器码。4.执行:运行机器码。V8引擎通过即时编译和隐藏类优化,SpiderMonkey使用类型推断系统,导致在相同代码上的性能表现不同。

探索前端中类似VSCode的面板拖拽调整功能的实现在前端开发中,如何实现类似于VSCode...
