Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This pull request adds a new utility,
atomTree
, which allows us to manage hierarchical Jotai atoms in a tree-like structure. It works similarly to Jotai’satomFamily
but provides subtree management, making it convenient to remove or inspect entire branches.Details
Creating Path Atoms
Creates (or retrieves) an atom at the specified path. Subsequent calls with the same path return the same atom instance.
Removing Path Atoms
Removes the atom at the specified path. If
removeSubTree
istrue
, all child paths under that path are also removed.This method removes the atom at the specified path. If
removeSubTree
istrue
, it also removes all child paths under that path.Retrieving A Subtree
Retrieves the internal node representing the specified path. This is useful for inspecting the tree structure. The node structure is as follows:
Retrieving A Node Path
Returns an array of node objects from the root node to the node at the specified path, inclusive.
Why we need this
atomTree
provides a convenient way to handle nested states, removing entire subtrees when they are no longer needed.How it works
atomTree
maintains a root object of typeNode
, having aMap
for children and an optionalatom
reference.Please review and let me know if you have any suggestions or concerns. Thank you!
Credit: For the inspiration, thank you @paralin