generated from gravity-ui/package-example
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability to use external values for shared context (#240)
- Loading branch information
1 parent
d55f030
commit d1b69cb
Showing
4 changed files
with
34 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,24 @@ | ||
import React from 'react'; | ||
|
||
export const useFormSharedStore = () => { | ||
const [store, setStore] = React.useState({}); | ||
export const useFormSharedStore = (shared?: Record<string, any>) => { | ||
const firstRender = React.useRef(true); | ||
const [store, setStore] = React.useState(shared || {}); | ||
|
||
const onChangeShared = React.useCallback( | ||
(name: string, value: any) => setStore((s) => ({...s, [name]: value})), | ||
[setStore], | ||
); | ||
|
||
React.useEffect(() => { | ||
if (firstRender.current) { | ||
firstRender.current = false; | ||
} else if (shared) { | ||
setStore({ | ||
...store, | ||
...shared, | ||
}); | ||
} | ||
}, [shared]); | ||
|
||
return {store, onChangeShared}; | ||
}; |
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 |
---|---|---|
@@ -1,12 +1,24 @@ | ||
import React from 'react'; | ||
|
||
export const useViewSharedStore = () => { | ||
const [store, setStore] = React.useState({}); | ||
export const useViewSharedStore = (shared?: Record<string, any>) => { | ||
const firstRender = React.useRef(true); | ||
const [store, setStore] = React.useState(shared || {}); | ||
|
||
const onChangeShared = React.useCallback( | ||
(name: string, value: any) => setStore((s) => ({...s, [name]: value})), | ||
[setStore], | ||
); | ||
|
||
React.useEffect(() => { | ||
if (firstRender.current) { | ||
firstRender.current = false; | ||
} else if (shared) { | ||
setStore({ | ||
...store, | ||
...shared, | ||
}); | ||
} | ||
}, [shared]); | ||
|
||
return {store, onChangeShared}; | ||
}; |