Design: Adding a createReducerSelector #1095
Unanswered
christiankaseburg
asked this question in
Q&A
Replies: 1 comment 3 replies
-
You may want to consider |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I have come across a problem of updating many atoms at once and being able to share the logic between components without adding more unnecessary overhead. Recently I have switched from redux to Recoil and started working on a reducer like pattern to manage complex state mutations. My main goal is to be able to maintain the logic for these complex state updates in a single place so they can be shared across components via recoil hooks.
Please give me some feedback if you know of better alternatives than this approach.
useReducerRecoilState - A wrapper function around useRecoilState for exposing the correct types.
createReducerSelector - Utility function for creating reducer selectors.
createReducerSelector.ts
Simple example using an atom that manages a list of tasks where each task atom is subscribable by id to prevent unnecessary updates in other components. For the simplicity of this example I am not including a atom effect to manage syncing the state with browser storage.
atoms.ts
selectors.ts
Now, I have a reducer example where I will manage deletions of tasks.
Reducer Example
taskListReducerSelector.ts
Usage example
As you can see I have a task list atom that manages a reference to my collection of task atoms by id. When I choose to remove a task from my list atom I will need to reset a task atom or if I want to add an atom I could add some overrides for setting the default state of a new task atom. My point is that managing state can get complicated when you need to add, remove, load, or update many atoms at once.
Does this seem like a weird pattern? Should I encapsulate this logic in a component and pass the functionality down to children components via props?
Note: I wrote all this in this discussion so some of the examples may contain typos or logic errors, but this is just a simple example showing a use case of this pattern.
Beta Was this translation helpful? Give feedback.
All reactions