-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
108 additions
and
77 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 was deleted.
Oops, something went wrong.
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,58 @@ | ||
import { useState, useEffect } from 'react'; | ||
/** | ||
* Factory function for creating React shared-state hooks. | ||
* | ||
* If you want to share some state across components and `props` propagation | ||
* or "Context" isn't cutting it, then this is probably what you're after. A | ||
* simple, easy, way to share common state across any number of components. | ||
* | ||
* # Example | ||
* | ||
* // In "app.js" (or wherever) create the hook, passing an initial value | ||
* export const useFoo = sharedStateHook('hello'); | ||
* | ||
* // Import hook | ||
* import {useFoo} from 'app.js'; | ||
* | ||
* function SomeComponent(props) { | ||
* // Use hook as you would `useState()` (sans the initial value) | ||
* const [foo, setFoo] = usefoo(); | ||
* | ||
* // `foo` is shared by all components that use this hook | ||
* // `setFoo` sets `foo` for all components that use this hook | ||
* ... | ||
* } | ||
* | ||
* # ... but why not React Context? | ||
* | ||
* 1. The Provider / Consumer model doesn't easily accomodate components that both | ||
* provide and consume. | ||
* 2. Contexts require coercing state into the React DOM, which isn't always | ||
* convenient. | ||
* 3. Contexts push developers toward a model where the context is a single "catchall" | ||
* Object that holds many, largely unrelated, properties. This leads to components | ||
* that depend on just one of the properties in a context, but that rerender when | ||
* *any* property is changed. | ||
* | ||
* @param {any} value | ||
* @returns {Function} React hook function | ||
*/ | ||
|
||
export default function(value, name /* for debugging */) { | ||
const setters = new Set(); | ||
|
||
return function useSharedState() { | ||
const [val, setVal] = useState(value); | ||
|
||
useEffect(() => { | ||
setters.add(setVal); | ||
return () => setters.delete(setVal); | ||
}, [val]); | ||
|
||
return [val, v => { | ||
// console.log(name, '->', v); | ||
value = v; | ||
for (const setter of setters) setter(value); | ||
}]; | ||
}; | ||
} |
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