Created
May 18, 2020 07:16
-
-
Save ChristopherBiscardi/0f36ceee4adfba47d08e415355f154c4 to your computer and use it in GitHub Desktop.
recoil counter
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
import React, { useState } from "react"; | |
export const Counter = (props) => { | |
const [count, setCount] = useState(0); | |
return ( | |
<div> | |
<input readOnly value={count} /> | |
<button | |
onClick={(e) => { | |
setCount(count + 1); | |
}} | |
> | |
Count | |
</button> | |
</div> | |
); | |
}; |
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
import React from "react"; | |
import { RecoilRoot, useRecoilState, atom } from "recoil"; | |
const counter = atom({ | |
key: "counter", | |
default: 0, | |
}); | |
export const Counter = (props) => ( | |
<RecoilRoot> | |
<CounterComponent /> | |
</RecoilRoot> | |
); | |
const CounterComponent = (props) => { | |
const [count, setCount] = useRecoilState(counter); | |
return ( | |
<div> | |
<input readOnly value={count} /> | |
<button | |
onClick={(e) => { | |
setCount(count + 1); | |
}} | |
> | |
Count Recoil | |
</button> | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment