Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(scope): Add tips gathered from common issues #29

Merged
merged 1 commit into from
Apr 30, 2024
Merged

Conversation

yf-yang
Copy link
Collaborator

@yf-yang yf-yang commented Apr 30, 2024

Add some documentation explaining problems of #25 #28

Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@yf-yang yf-yang requested a review from dai-shi April 30, 2024 04:13
@yf-yang yf-yang merged commit fc73a3a into main Apr 30, 2024
3 checks passed

Derived atoms are merely proxies of primitive atoms. When scoping a derived atom, although the atom itself is copied, all of those copies still reference the same primitive atom, so their values are still shared.

It is a common pattern in Jotai to wrap the primitive atom within a utility function. To scope it properly, you should expose the primitive atom and then apply the scoping to it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should encourage exposing base atoms. They are an implementation detail of the custom atom. Most developers use custom atoms from libraries out of the box. How would I scope atoms like atomWithMachine?


NOTE: Async atoms always have side effects. To handle it, add additional code to prevent extra side effects. You can check this [issue](https://github.com/jotaijs/jotai-scope/issues/25#issuecomment-2014498893) as an example.

2. Scoping a derived atom has no behavioral effect. Avoid it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid it.

If this pattern is to be prohibited, then why not solve this with typescript?


## Pro Tips

1. Within a `ScopeProvider`, although an atom may not be scoped, its `atom.read` function could be called multiple times. **Therefore, do not use `atom.read` to perform side effects.**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be called multiple times

Why is this?

@yf-yang yf-yang deleted the add-tips branch May 7, 2024 05:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants