atomWithRefresh example from the docs doesn't work #2995
-
Bug DescriptionExample in the docs suggests we pass the refresh function like this:
It results in a React's click event being implicitly passed to the setter. The setter checks if arguments are empty, and they are not. It then assumes we're trying to set something into the atom rather than refresh it. But the write function is not passed therefore it does nothing. It looks like we need to change the order of conditions here like this so that it only cares about its arguments if it is writeable An alternative is just to update the docs and tell people to wrap refresh functions into lambdas. However, this won't stop people from trying and getting confused. Especially since even Typescript allows to coerce function types like that and doesn't complain. Another alternative would be to split atomWithRefresh into 2 functions: one writeable and the other read-only. It would save us 1 tricky if-else and prevent bugs like these in the future. However, it would introduce unnecessary breaking changes and make it harder for the users to remember. Reproduction Linkhttps://stackblitz.com/edit/vitejs-vite-yeqmd4n4?file=src%2FApp.tsx |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Nice catch. I think it's the doc issue. <button type="button" onClick={() => refreshPosts()}> We shouldn't pass event anyways. |
Beta Was this translation helpful? Give feedback.
Yeah, let's throw an error in DEV mode.
Can you modify #2996 for this?