Skip to content

Instantly share code, notes, and snippets.

@ChristopherBiscardi
Created May 18, 2020 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChristopherBiscardi/0f36ceee4adfba47d08e415355f154c4 to your computer and use it in GitHub Desktop.
Save ChristopherBiscardi/0f36ceee4adfba47d08e415355f154c4 to your computer and use it in GitHub Desktop.
recoil counter
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>
);
};
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