keajs / kea-next-test

test repository to experiment with next.js and kea
1 stars 0 forks source link

Beta 20 -> context api & getStore #1

Open etiennecl opened 5 years ago

etiennecl commented 5 years ago

I've just seen that you added a new context api to kea. I've tried updating the example with the new versions and updating the example with these modifications in the store:

export const initStore = (initialState) => {
  resetContext()
  return getStore({ preloadedState: initialState })
}

On SSR the store doesn't seems to get the initialState, but after that everything seems to be working. When navigating to another route, the logic doesn't get connected to the component, probably because of the restContext() being inside of the initStore function.

I've tried lazymounted the logic but without success and calling resetContext() outside the initStore fucntion but then the store is undefined.

mariusandra commented 5 years ago

Hi @etiennecl

The latest code in the master branch of this repo seems to already work a lot better that before. I still did not try adding sagas, but managed to fix many other issues.

The biggest problem I had was that when changing routes, everything would crash. I traced it down to something still wanting to render the old page even though it has already been removed from redux:

image

So I added two new properties to context to try something:

  resetContext({
    attachStrategy: 'replace',
    detachStrategy: 'lazy'
  })

They toggle what happens when a logic is mounted to redux.

They work like this:

With the detachStrategy at lazy, the code that for some reason renders the old route after the new one has been mounted, will be satisfied as it will (hopefully?) still find what it needs in the store. Due to memoization nothing will actually be rendered anyway.

It feels kind of hacky, but it seems to work for now.

Some other things I did with:

So many changes... and it seems to work quite well.

However like I said, I still didn't get the chance to try out redux-saga. That's for another day... unless you manage to get it working first :).