esamattis / redux-hooks

⚓ React Hooks implementation for Redux
http://npm.im/@epeli/redux-hooks
MIT License
96 stars 4 forks source link

feat request: can you export the StoreContext? #10

Open gcloeval opened 5 years ago

gcloeval commented 5 years ago

Is it possible to export the StoreContext interface, so that we can access the store via standard useContext, i.e.: const store = useContext(StoreContext);

Similar to how its done here? https://github.com/facebookincubator/redux-react-hook#storecontext

Thanks for the library!

esamattis commented 5 years ago

Sure. May I ask what's your use case for it?

gcloeval commented 5 years ago

Yeah sure- I have a few legacy client service wrappers that I am converting to be accesible via hooks. Those take in a store as a constructor, then dispatch methods, something like:

export class RdxClient {
  constructor(public store: Store) {
    console.log('new rdx client constructored, store=', store)
  }
 createQuery(requestId: string, query: IQueryRdx) {
 this.store.dispatch(setLeafQuery(requestId, query))
}
}

now I just expose them via hooks:

import { useContext } from 'react'
import { MyStoreContext } from './es-hooks-provider'
import { RdxClient } from '../elastic-ts-dsl-search/rdx-client'
export function useRdxClient(): RdxClient {
  const { store } = useContext<any>(MyStoreContext)
  const rdxClient = new RdxClient(store)
  return rdxClient
}

in this example, i have just added an extra provider with "MyStoreContext" before wrapping yours:

  <MyStoreContext.Provider value={{ store: store }}>
      <HooksProvider store={store}>{props.children}</HooksProvider>
    </MyStoreContext.Provider>

but if you just export your own context I wont need it and can just lift the store from your provider.