-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'use-local-state-store'
- Loading branch information
Showing
9 changed files
with
198 additions
and
33 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
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,22 @@ | ||
import { Store } from "./Store"; | ||
import { useRef } from "react"; | ||
import isEqual from "fast-deep-equal/es6"; | ||
|
||
function useLocalStore<S>(initialState: (() => S) | S, deps?: ReadonlyArray<any>): Store<S> { | ||
const storeRef = useRef<Store<S>>(); | ||
|
||
if (storeRef.current == null) { | ||
storeRef.current = new Store(typeof initialState === "function" ? (initialState as any)() : initialState); | ||
} | ||
|
||
if (deps !== undefined) { | ||
const prevDeps = useRef<ReadonlyArray<any>>(deps); | ||
if (!isEqual(deps, prevDeps)) { | ||
storeRef.current = new Store(typeof initialState === "function" ? (initialState as any)() : initialState); | ||
} | ||
} | ||
|
||
return storeRef.current; | ||
} | ||
|
||
export { useLocalStore }; |
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,21 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Test Minified</title> | ||
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> | ||
<script crossorigin src="https://unpkg.com/immer@7.0.1/dist/immer.umd.production.min.js"></script> | ||
<script src="../dist/pullstate.umd.min.js" type="text/javascript" charset="utf-8"></script> | ||
</head> | ||
<body> | ||
<h1>Hi</h1> | ||
<script type="text/javascript"> | ||
const TestStore = new pullstate.Store({ | ||
hi: "hello", | ||
}); | ||
|
||
console.log(TestStore); | ||
|
||
TestStore.subscribe(s => s.hi, (text) => console.log(text)); | ||
</script> | ||
</body> | ||
</html> |
Oops, something went wrong.