Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react/utils): Add useProxy() hook (#626)
* Add useProxy() * Run prettier * add entry point and so on * Added important info to the jsdoc * deprecate useProxy macro Co-authored-by: daishi <daishi@axlight.com>
- Loading branch information
1 parent
2fab622
commit 6aa5e8a
Showing
6 changed files
with
63 additions
and
34 deletions.
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
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
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
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
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 @@ | ||
export { useProxy } from './react-utils/useProxy' |
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,40 @@ | ||
import { useLayoutEffect } from 'react' | ||
import { useSnapshot } from '../index' | ||
|
||
/** | ||
* useProxy | ||
* | ||
* 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.: | ||
* | ||
* export const store = proxy(initialState) | ||
* export const useStore = () => useProxy(store) | ||
* // in the component file: | ||
* function Cmp() { | ||
* const store = useStore() | ||
* return <button onClick={() => {store.count++}}>{store.count}</button> | ||
* } | ||
* | ||
* @param proxy | ||
* @returns A new proxy which you can use in the render as well as in callbacks. | ||
*/ | ||
export function useProxy<T extends object>(proxy: T): T { | ||
const snapshot = useSnapshot(proxy) as T | ||
|
||
let isRendering = true | ||
|
||
useLayoutEffect(() => { | ||
// This is an intentional hack | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
isRendering = false | ||
}) | ||
|
||
return new Proxy(proxy, { | ||
get(target, prop) { | ||
return isRendering ? snapshot[prop as keyof T] : target[prop as keyof T] | ||
}, | ||
}) | ||
} |
6aa5e8a
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.
Successfully deployed to the following URLs:
valtio – ./
valtio-pmndrs.vercel.app
valtio.pmnd.rs
valtio-git-main-pmndrs.vercel.app
valtio.vercel.app