React redux 的#applyMiddleware 如何确保中间件仅由 return #createStore 而不是商店应用一次?

How does react redux's #applyMiddleware ensure the middleware is only applied once by return the #createStore instead of a store?

更改后的签名如何防止您多次应用中间件?它returns一个新的#createStore,但是这个方法可以在另一个#applyMiddleware中再次用作参数,就像可以再次使用store一样,导致重复的中间件。

To ensure that you may only apply middleware once, it operates on createStore() rather than on store itself. Instead of (store, middlewares) => store, its signature is (...middlewares) => (createStore) => createStore.

好问题!我也不太明白这是多么明显,但我认为它大致是这样工作的:

  1. applyMiddleware() 正如您指出的那样接受 createStore() 函数。每次调用 applyMiddleware() 时都会调用 createStore()。所以,每次新店都是运行.
  2. createStore() 接受专门的增强器,这些增强器是添加到商店实例的东西。 applyMiddleware() 会提供的东西。
  3. 如果 createStore() 被赋予了增强器,它(间接)递归调用 createStore() 而没有任何增强器。因此增强剂仅使用一次。

我认为不止一次应用中间件的唯一方法是实现您自己的 createStore() 版本。但是到那时你可能会得到一个循环引用。