Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upWhy Provider component and context needed? #719
Comments
This comment has been minimized.
This comment has been minimized.
Yes, it would be possible to explicitly import the store everywhere, but that makes testing and SSR much more difficult. See http://redux.js.org/docs/faq/StoreSetup.html#store-setup-multiple-stores . Since |
markerikson
closed this
Jun 12, 2017
This comment has been minimized.
This comment has been minimized.
I think you misunderstood my question. I am asking why // provider.js (in react-redux package - Hypothetically)
let store;
const registerStore = (store) => store = store;
const getStore = () => store;
export { registerStore, getStore };
// connect.js (in react-redux package - Hypothetically)
import { getStore } from './provider';
// same connect code but just use getStore to access to the store.
// index.js
import { createStore } from 'redux';
import { registerStore } from 'react-redux';
const store = createStore(rootReduer, enhancer);
registerStore(store);
... |
This comment has been minimized.
This comment has been minimized.
Singletons, especially stateful ones, are usually an antipattern. This especially would be bad for server-side. |
This comment has been minimized.
This comment has been minimized.
Yep - the linked FAQ entry discusses this. |
This comment has been minimized.
This comment has been minimized.
Thanks for your answers. |
alisabzevari commentedJun 12, 2017
As I understood from the source code of
Provider
andconnect
,Provider
acts as a dependency container that holds the store and makes it available for theconnect
. My question is, why it should be a component and why it should use React's context at all?Wouldn't it be possible to just define a module that holds the created store as a local variable in the module?