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

Integrating into existing redux apps -> how to reuse existing store? #103

Closed
AnsonT opened this issue Nov 7, 2019 · 1 comment
Closed

Comments

@AnsonT
Copy link

AnsonT commented Nov 7, 2019

For an existing application that have a store created with redux/createStore. How should that store be integrated into kea?

@mariusandra
Copy link
Member

Hey, sorry for the long wait with the answer... and it might not be what you want to hear :/

Unfortunately this is an approach that I don't recommend. When initialising the store, kea does many things that are hard to replicate in your own code, including for example any setup that the various plugins might want to run.

It's often easier to convert your own store setup to the format expected by resetContext({ createStore: { ... } }). It might even help clean up the otherwise relatively messy default redux store creation code. See here for the API of createStore.

However, if you really really want to do this, basically the minimum you need is to run:

// still, I don't recommend it
import { resetContext, keaReducer } from 'kea'

function runPlugins (key, ...args) {
  const { plugins } = getContext()
  plugins && plugins.events[key] && plugins.events[key].forEach(p => p(...args))
}

const options = { createStore: false }
const createStoreOptions = {}
const context = resetContext(options)
runPlugins('beforeReduxStore', createStoreOptions)
context.store = store
runPlugins('afterReduxStore', createStoreOptions, store)

When you create a store you will also need to pass it reducers that will contain the data kea will use.

import { keaReducer } from 'kea'
const reducers = { 
  // your own reducers here
  kea: keaReducer('kea'),
  scenes: keaReducer('scenes'),
  // other keaReducers if needed
}

const store = finalCreateStore(combineReducers(reducers), ...)

NB, I didn't test this code, but just extracted it from the store setup. Read the original source in case something is broken.

Also, it's possible that instead of Redux's combineReducers you will need to run combineKeaReducers, but that's also not exported publicly and it might be easier to just copy/paste the code to your app.

That should be it... :)

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

2 participants