7guis, task1. Counter in regular React with useState
vs a RecoilJS implementation.
Important notes here:
atom
and RecoilRoot
), Recoil is drop-in replacement for useState
import React, { useState } from "react";export const Counter = (props) => {const [count, setCount] = useState(0);return (<div><input readOnly value={count} /><buttononClick={(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} /><buttononClick={(e) => {setCount(count + 1);}}>Count Recoil</button></div>);};