简体   繁体   English

使用Redux的store.getState()和store.dispatch()进行更改而无需订阅React?

[英]Using Redux's store.getState() and store.dispatch() to make changes without subscribing in React?

I watched the Getting Started with Redux series and am working through modifying my existing React code to use Redux. 我观看了Redux入门系列,并正在通过修改现有的React代码来使用Redux。 However, I've run into a dilemma that I can't seem to find any answers for: I want to modify the store data (which many of my React components will use) on particular events such UI interaction, but I don't want any UI components to subscribe to every change made to the store. 但是,我陷入了一个困境,似乎找不到任何答案:我想在特定的事件(如UI交互)上修改商店数据(我的许多React组件将使用该商店数据),但是我没有希望任何UI组件都可以订阅对商店所做的所有更改。

Can/should I instead make a separate .js file with functions that can access the store directly to call .getState() and .dispatch() which my UI components can import and call when needed, instead of subscribing to the store themselves? 我是否可以/应该制作一个单独的.js文件,该文件具有可以直接访问商店的函数来调用.getState()和.dispatch(),我的UI组件可以在需要时导入和调用这些函数,而无需订阅商店本身?

Or am I fundamentally misunderstanding something and subscribing to the store would have exactly the same effect? 还是我从根本上误会了一些东西,订阅这家商店会产生完全相同的效果? I'm aiming to optimize speed; 我的目标是优化速度; that's why I switched to Redux. 这就是为什么我切换到Redux的原因。

I want to modify the store data (which many of my React components will use) on particular events such UI interaction, but I don't want any UI components to subscribe to every change made to the store. 我想在诸如UI交互之类的特定事件上修改商店数据(我的许多React组件将使用许多商店数据),但是我不希望任何UI组件订阅对商店所做的每项更改。

Checkout react-redux , it's the official binding for react and redux apps. Checkout react-redux ,它是react和redux应用程序的正式绑定。 It provides a way to subscribe specific components to redux and only listen for the changes that you are interested in. On top of that it also implements various optimisations to avoid unnecessary re-renders. 它提供了一种方法,可以将特定的组件订阅到redux,并且仅侦听您感兴趣的更改。最重要的是,它还实现了各种优化以避免不必要的重新渲染。

Can/should I instead make a separate .js file with functions that can access the store directly to call .getState() and .dispatch() which my UI components can import and call when needed, instead of subscribing to the store themselves? 我是否可以/应该制作一个单独的.js文件,该文件具有可以直接访问商店的函数来调用.getState()和.dispatch(),我的UI组件可以在需要时导入和调用这些函数,而无需订阅商店本身?

You can do anything you like, but don't be surprised if it becomes unmaintainable after a while. 您可以做任何喜欢的事情,但是如果一段时间后变得无法维护,请不要感到惊讶。

Or am I fundamentally misunderstanding something and subscribing to the store would have exactly the same effect? 还是我从根本上误会了一些东西,订阅这家商店会产生完全相同的效果?

Yes, you could use react-redux and only subscribe to parts of the store that you care about. 是的,您可以使用react-redux并仅订阅您关心的部分商店。

I'm aiming to optimize speed; 我的目标是优化速度; that's why I switched to Redux. 这就是为什么我切换到Redux的原因。

Not sure what your particular situation is / was, but in general the point of redux is to provide a predictable and manageable way of maintaining an applications state - it doesn't directly speed up anything. 不确定您的特定情况是/曾经是什么,但是总的来说,redux的目的是提供一种可预测且可管理的方式来维持应用程序状态-它不会直接加速任何事情。

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

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