Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Static reducers in nested path disappears after attaching dynamic reducer. #424

Closed
lubomir-chatoryiskyi opened this issue Oct 29, 2020 · 8 comments

Comments

@lubomir-chatoryiskyi
Copy link

Is it a bug, feature request or question?

Question (or bug)

Which package(s) does this involve?

[@redux-dynostore/core]

Input Code

store.attachReducers({ 'some.path.dynamicReducer1': dynamicReducerFunction });

Current Behavior

Hi! I'm trying to attach dynamic reducer to the store to a nested path. The issue is that in this path I already have static reducers and I'd like to attach a dynamic one. But after attaching static reducers disappears from the state.

some: {
   path: {
       staticReducer1: reducerFuction1,
       staticReducer2: reducerFuction2
   }
}

after calling store.attachReducers({ 'some.path.dynamicReducer1': reducerFuction }); static reducers disappears and I have

some: {
   path: {
       dynamicReducer1: reducerFuction,
   }
}

Expected result:

some: {
   path: {
       staticReducer1: reducerFuction1,
       staticReducer2: reducerFuction2,
       dynamicReducer1: reducerFuction,
   }
}

Your Setup

"redux": "^4.0.5",
"react-redux": "^7.1.3",
"@redux-dynostore/core": "^3.1.0",

@nnnoel
Copy link

nnnoel commented Nov 5, 2020

I've also encountered this issue. I've also tried specifying the state handler when attaching a reducer, but the custom one doesn't appear to get used.

Configuring the deep state handler in the reducer enhancement worked for me.

import dynostore, { dynamicReducers, deepStateHandler } from "@redux-dynostore/core";

dynostore(dynamicReducers({ stateHandler: deepStateHandler }))

@nnnoel
Copy link

nnnoel commented Nov 5, 2020

@mpeyper The following reproduces this in the integrations spec, though I'm not sure how to fix it. It has something to do with options on this line taking hierarchal precedence.

const expandedReducers = { reducer: staticReducer, children: {}, options: defaultOptions }

  test('should attach dynamic reducer to static key with state handler override', () => {
    const reducer = combineReducers({
      group1: combineReducers({
        group2: combineReducers({
          key1: makeTestReducer('key1')
        }),
        key3: makeTestReducer('key3')
      }),
      key5: makeTestReducer('key5')
    })

    const store = createStore(reducer, dynostore(dynamicReducers()))

    store.attachReducers({
      group1: {
        group2: {
          key2: makeTestReducer('key2')
        },
        key4: makeTestReducer('key4')
      }
    }, { stateHandler: deepStateHandler })

    expect(store.getState()).toEqual({
      group1: {
        group2: {
          key1: 'key1 - initialValue',
          key2: 'key2 - initialValue'
        },
        key3: 'key3 - initialValue',
        key4: 'key4 - initialValue'
      },
      key5: 'key5 - initialValue'
    })
  })

@mpeyper
Copy link
Contributor

mpeyper commented Nov 10, 2020

Thanks for raising @lubomir-chatoryiskyi and investigating a bit @nnnoel.

In general, attaching dynamic reducers to a static reducer node is not allowed with the default state handler (shallow). As @nnnoel discovered, the deep state handler is supposed to allow this, but there are some known issues with using it that have never been resolved and we currently advise against it. Honestly, I think the concept was flawed from the get go and I don't have any ideas for resolving it.

If possible, I'd recommend separating the static reducers from the dynamic reducers at the root of the store, or if that is not possible, creating the store with an empty reducer and attach the static reducers immediately after creation. I'm aware this is not ideal.

@burakakca
Copy link

burakakca commented Jan 4, 2021

Any improvement? I am also facing the same problem in this regard. I need to add dynamically nested reducers.

@mpeyper
Copy link
Contributor

mpeyper commented Jan 5, 2021

No changes for this @burakakca. Honestly, I'm not sure how to solve this one in general.

@burakakca
Copy link

I'm asking not for this topic but for a quick response. So how can I access a different subspace state from different subspace.

@mpeyper
Copy link
Contributor

mpeyper commented Jan 7, 2021

@burakakca the general answer is that you don't and if you do need it then perhaps you need to reconsider the boundaries of your subspaces or the use of this library all together. There are some exceptions, namely global state, which is covered in the redux-subspace docs

Please raise a new issue if you want to discuss this further.

@mpeyper
Copy link
Contributor

mpeyper commented Jan 27, 2021

Closing. Please see #484 for details.

@mpeyper mpeyper closed this as completed Jan 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants