-
-
Save josefrichter/66e7c6a2e83d07e2e75b0a1cac9f4e95 to your computer and use it in GitHub Desktop.
Revisions
-
koenbok revised this gist
Apr 15, 2019 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,10 +4,16 @@ import { useState, useEffect } from "react"; /** A hook to simply use state between components Usage: // You can put this in an central file and import it too const useStore = createStore({ count: 0 }) // And this is how you use it from any component export function Example() { const [store, setStore] = useStore() const updateCount = () => setStore({ count: store.count + 1 }) return <div onClick={updateCount}>{store.count}</div> } */ -
koenbok revised this gist
Apr 2, 2019 . 1 changed file with 18 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,10 @@ import * as React from "react"; import { useState, useEffect } from "react"; /** A hook to simply use state between components Usage: const useStore = createStore({ count: 0 }) export function Example() { const [store, setStore] = useStore() const updateCount = () => setStore({ count: store.count + 1 }) @@ -16,20 +13,20 @@ export function Example() { */ export function createStore<T>(state: T) { let storeState: T = Object.assign({}, state); const storeSetters = new Set(); const setStoreState = (state: Partial<T>) => { storeState = Object.assign({}, storeState, state); storeSetters.forEach(setter => setter(storeState)); }; function useStore(): [T, typeof setStoreState] { const [state, setState] = useState(storeState); useEffect(() => () => storeSetters.delete(setState), []); storeSetters.add(setState); return [state, setStoreState]; } return useStore; } -
koenbok revised this gist
Apr 2, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ import * as React from "react" import { useState, useEffect } from "react" /** A hook to simply use state between components -
koenbok created this gist
Mar 31, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ import * as React from "react" import { useState, useEffect, useMemo } from "react" /** A hook to simply use state between components Usage: const useStore = createStore({ count: 0 }) export function Example() { const [store, setStore] = useStore() const updateCount = () => setStore({ count: store.count + 1 }) return <div onClick={updateCount}>{store.count}</div> } */ export function createStore<T>(state: T) { let storeState: T = Object.assign({}, state) const storeSetters = new Set() const setStoreState = (state: Partial<T>) => { storeState = Object.assign({}, storeState, state) storeSetters.forEach(setter => setter(storeState, storeState)) } function useStore(): [T, typeof setStoreState] { const [state, setState] = useState(storeState) useEffect(() => () => storeSetters.delete(setState), []) storeSetters.add(setState) return [state, setStoreState] } return useStore }