question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion: namespaced reducers / actions

See original GitHub issue

Hi!

I love redux and I love reducers that filter actions by a namespace and created my own namespaced-duck-factory library because of that.

It seems that redux-toolkit could make my lib obsolete so I would love to have the option of creating state slice reducers that filter actions by a { meta: ‘namespace’ } property (for example).

If I understand it correctly it would be a little extension of createSlice() so it also emits namespaced action-creators.

Thanks for reading 😃

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
soundyogicommented, Nov 3, 2020

Sorry to comment such an old thread.

As It turns out. redux-toolkit did indeed made it unneccessary to have preconfigured slices. (I also never really needed the namespacing) Especially with the entityAdapter and immer out of the box.

So this library gave me so much joy and makes my life easier.

I even managed to introducce it to a context / hooks only codebase for a large client and it works very well so far - we use it to make redux-less redux possible (by using it with useReducer() and some middleware)

So thanks and:

Happy Christmas (well, soon-ish) 😄 @phryneas

3reactions
phryneascommented, Dec 7, 2019

Heyo 😃

createSlice already namepaces all actions going with the slice - so an increment action on a counter slice would be of type counter/increment.

So if you were to namepace that again, I believe it would be more appropriate to namespace the type one level deeper (creating types like namespace/counter/increment) instead of using meta for that - as reducers usually decide solely by type if they’re responsible for handling an action and many established patterns (and interop with many other libraries) break if this rule is ignored.

If you wanted to do that, there’s nothing speaking against creating some kind of “higher-order-slice” like this:

function createCounterSlice(namespace: string) {
  return createSlice({
    name: `${namespace}/counter`,
    initialValue: 0,
    reducers: { increment(state){ state++; }  }
  });
}
const clickCounterSlice = createCounterSlice('click');
console.log(clickCounterSlice.action.increment()) // { type: 'click/counter/increment' }

So I think your use case should already be covered - would that work for you?

PS: I love the logo of your library - childhood memories!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Namespacing Actions for Redux - Kickstarter Engineering
When an action is dispatched, each reducer is called with that action and the slice of state which corresponds to its name.
Read more >
Redux module composition and how to avoid namespacing
redux-form, react-redux-toastr) The module exposes a function that given a prefix returns already namespaced reducers and actions.
Read more >
Redux store namespaced actions? - reactjs - Stack Overflow
Whenever I want to create a new view I can inherit this base view. All possible actions are shared in the reducer. The...
Read more >
Namespacing Redux Action Type Constant Values
Namespacing Redux Action Type Constant Values. Most everyone agrees that defining constants for your Redux action types is good idea.
Read more >
Namespacing Redux Action Type Constant Values | by Cliff Hall
If your action types are defined in separate files, chances are the reducers that respond to them are as well.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found