-
-
Notifications
You must be signed in to change notification settings - Fork 235
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
Documentation improvement and support for V2 #869
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
title: 'useProxy' | ||
section: 'API' | ||
subSection: 'Utils' | ||
description: '' | ||
--- | ||
|
||
```tsx | ||
/** | ||
* Takes a proxy and returns a new proxy which you can use in both react render | ||
* and in callbacks. The root reference is replaced on every render, but the | ||
* keys (and subkeys) below it are stable until they're intentionally mutated. | ||
* For the best ergonomics, you can export a custom hook from your store, so you | ||
* don't have to figure out a separate name for the hook reference. E.g.: | ||
* | ||
* @param proxy | ||
* @param options | ||
* @returns A new proxy which you can use in the render as well as in callbacks. | ||
*/ | ||
``` | ||
|
||
```tsx | ||
// store.tsx | ||
export const store = proxy(initialState) | ||
export const useStore = () => useProxy(store) | ||
|
||
// SomeComponent.tsx | ||
const SomeComponent: React.FC = () => { | ||
const store = useStore() | ||
|
||
return <button onClick={() => { store.count++ }}>{store.count}</button> | ||
} | ||
``` |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--- | ||
title: 'Sync vs async observer' | ||
description: 'Understand the difference between synchronous and asynchronous updates when using valtio observer' | ||
--- | ||
import eventLoopSrc from './event_loop.webp' | ||
|
||
## Subscribe : Synchronous vs asynchronous updates | ||
|
||
When using subscribe based observer, the callback might be called synchronously or asynchronously. | ||
|
||
But before this, we need a quick reminder of the event loop behavior in JavaScript, the following schema is a simplified version of the event loop for NodeJS, browser implemention is similar. | ||
|
||
<img src={eventLoopSrc} alt="eventloop" /> | ||
<div className='text-center text-xs'>Schema copyrights from builder.io</div> | ||
|
||
After each callback the JS engine check for callbacks in the microtask queue. If there are some, it will execute them before the next iteration of the event loop. | ||
|
||
In valtio, each time a proxy update is notified if the mode is async (default) it will be deferred to the microtask queue, thus allowing batching of state. | ||
|
||
When using `subscribe`, the third argument defines if the callback should be called synchronously or asynchronously. If `true` the callback will be called synchronously, if `false` it will be called asynchronously. | ||
|
||
Let's see an example | ||
```tsx | ||
import { proxy, subscribe } from "valtio/vanilla"; | ||
|
||
const store = proxy({ | ||
count: 0, | ||
}); | ||
|
||
subscribe( | ||
store, | ||
() => { | ||
console.log("Sync log", store.count); | ||
}, | ||
true | ||
); | ||
|
||
subscribe( | ||
store, | ||
() => { | ||
console.log("Batched log", store.count); | ||
}, | ||
false | ||
); | ||
|
||
// During the same tick | ||
store.count++; | ||
store.count++; | ||
|
||
// Will log | ||
// Sync log 1 | ||
// Sync log 2 | ||
// Batched log 2 | ||
``` | ||
|
||
Batching is generally more efficient and prefered as it regroups all the changes occuring during the same tick. But in some cases, you might want to have synchronous updates. A quick example is having a detailed log of the changes. When batching you'll only see the final result, but with synchronous updates you'll see each change. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
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.
It's a very handy util, but we are still not sure how we should suggest the usage. Our primary hook is
useSnapshot
anduseProxy
is a wrapper around it.