Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to obtain store in a component #948

Closed
sodik82 opened this issue May 12, 2018 · 3 comments
Closed

Ability to obtain store in a component #948

sodik82 opened this issue May 12, 2018 · 3 comments

Comments

@sodik82
Copy link

sodik82 commented May 12, 2018

Hi there,

this is most likely a question or enhancement proposal. I know that I could get store by using old context API - but this looks not to be part of official API. It looks to me like the internal implementation that can change (e.g. with new React Context implementation). Is there a official way to retrieve the store?

My motivation:
I am using redux-saga and I like the way how they "enhanced" store in their example in official API

export default function configureStore(initialState) {
  // Note: passing middleware as the last argument to createStore requires redux@>=3.1.0
  const sagaMiddleware = createSagaMiddleware()
  return {
    ...createStore(reducer, initialState, applyMiddleware(/* other middleware, */sagaMiddleware)),
    runSaga: sagaMiddleware.run
  }
}

I would like to execute runSaga in my components and therefore need get the store. And as store is already provided by Provider it would be convenient to able to retrieve it somewhere down the road in official way.

I know that this is not direct responsibility of react-redux but I believe other users could benefit from (or misuse) such feature as well :)

@markerikson
Copy link
Contributor

Yeah, accessing the store directly is definitely not part of the public React-Redux API. Now, because <Provider> currently puts the store into the old-style context, I've certainly seen plenty of third-party libraries that access context.store and do stuff with it (separate subscriptions, passing down a modified version of the store to descendants, etc).

When we switch to using the new context API provided by React 16.3, all that unofficial usage will break, because context.store will no longer exist. In addition, the current prototype doesn't even pass down the store to connected components, just the current store state and dispatch. I'm still trying to figure out the best way to support this kind of use case, especially for the kind of situation that you're describing - React components that dynamically add slice reducers or kick off sagas.

That said, there's certainly nothing stopping you from having your own context-based method of passing down the store to your own components.

@sodik82
Copy link
Author

sodik82 commented May 13, 2018

sure, there are definitely ways around it.

I basically agree that standard use-case does not involve directly playing with store, so "easiest" solution might be to provide injectStore HOC (or Consumer as Provider counterpart) for such use-cases.

Thanks for considerations.

@timdorr
Copy link
Member

timdorr commented Jul 19, 2018

For now, you can use the legacy Context API to get access to it.

In the future, when we use the new Context API, we'll provide that context's Consumer as an export, so you can get to it that way.

@timdorr timdorr closed this as completed Jul 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants