-
-
Notifications
You must be signed in to change notification settings - Fork 696
investigate side-effect regression #3165
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
base: main
Are you sure you want to change the base?
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
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. |
commit: |
|
| Playground | Link |
|---|---|
| React demo | https://livecodes.io?x=id/GCEZ9WQ3E |
See documentations for usage instructions.
…Atoms (pmndrs#2982)" This reverts commit f5d843c.
32650fe to
f5edd30
Compare
|
/ecosystem-ci run |
Ecosystem CI Output |
I expected this. |
|
(I'll have a look later.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay. I agree. This shouldn't be merged.
| const previousValueAtom = atom() | ||
| const testAtom = atom((get) => { | ||
| const newValue = get(basicAtom) | ||
| store.set(previousValueAtom, newValue) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to support this.
If we can't support this, we should probably warn it (dev only) if we can detect it.
My suggestion is something like this: function atomShallowEqual(initialValue) {
const valueAtom = atom(initialValue);
return atom(
(get) => get(valueAtom),
(get, set, newValue) => {
const oldValue = get(valueAtom);
if (!shallowEqual(oldValue, newValue)) {
set(valueAtom, newValue);
}
}
);
}So, the challenge is how we can do with function atomShallowEqual(fn) {
let lastValue;
return atom((get) => {
const value = fn(get);
if (!shallowEqual(lastValue, value)) {
lastValue = value;
}
return lastValue;
});
}It still violates purity, but it feels like a better hack. |
Related Bug Reports or Discussions
#3164
Fixes #
Summary
I don't think we should merge this PR. I don't think Jotai should support side-effects in atom read.
The use-case which spawned this investigation was with
I think there could be a better pattern for above. I haven't found a pattern that supports side-effects in read.
Check List
pnpm run fixfor formatting and linting code and docs