-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from jimengio/use-atom
provide an useAtom hooks API
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React, { FC } from "react"; | ||
import { css } from "emotion"; | ||
import { useAtom } from "../src/use-atom"; | ||
|
||
let DemoAtom: FC<{ className?: string }> = React.memo((props) => { | ||
let dataAtom = useAtom({ a: 1 }); | ||
|
||
/** Plugins */ | ||
/** Methods */ | ||
/** Effects */ | ||
/** Renderers */ | ||
return ( | ||
<div className={props.className}> | ||
<pre>{JSON.stringify(dataAtom.deref(), null, 2)}</pre> | ||
<div> | ||
<button | ||
onClick={() => { | ||
dataAtom.resetWith({ a: 0 }); | ||
}} | ||
> | ||
Reset | ||
</button> | ||
</div> | ||
<div> | ||
<button | ||
onClick={() => { | ||
dataAtom.swapWith((data) => { | ||
data.a += 1; | ||
}); | ||
}} | ||
> | ||
Add | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
}); | ||
|
||
export default DemoAtom; |
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,25 @@ | ||
import { useState, useRef } from "react"; | ||
import { produce } from "immer"; | ||
|
||
/** "Clojure Atom"-like data management for React Hooks */ | ||
export let useAtom = <T>(data: T) => { | ||
// state only for triggering changes, data actually hold in dataRef | ||
let [state, setState] = useState(data); | ||
let dataRef = useRef(data); | ||
|
||
console.log("Atom demo updating"); | ||
|
||
return { | ||
deref: () => dataRef.current, | ||
resetWith: (newData: T) => { | ||
dataRef.current = newData; | ||
setState(newData); | ||
}, | ||
/** updates with Immer, returns a frozen result */ | ||
swapWith: (f: (d: T) => T | void) => { | ||
let newData = produce(dataRef.current, f) as T; | ||
dataRef.current = newData; | ||
setState(newData); | ||
}, | ||
}; | ||
}; |