Skip to content

helper to use react reducer function with a recoil atom

Notifications You must be signed in to change notification settings

NXML/use-atom-reducer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

use-atom-reducer

Example

import React from "react"
import {atom} from "recoil"
import {useAtomReducer} from "use-atom-reducer"


export const counterState = atom({
    key: 'counter', // unique ID (with respect to other atoms/selectors)
    default: 0, // default value (aka initial value)
  });



export default function Counter() {
    
    const reducer = (state,action)=>{
        if(action.type=="incr"){
            return state + action.payload
        }
        if(action.type=="decr"){
            return state - action.payload
        }

    }


    const [count,dispatch] = useAtomReducer(counterState,reducer)
    const handleIncr =()=>{
        dispatch({type:"incr",payload:1})
    }
     const handleDecr =()=>{
        dispatch({type:"decr",payload:1})
    }


    return <div>
        {count}
        <button onClick= {handleIncr}> INCREMENT  </button>
        <button onClick= {handleDecr}> DECREMENT  </button>
    </div>

}

App

import Counter from "./Counter"
import { RecoilRoot } from "recoil"


function App() {
  return (
    <div className="App">
     
        <RecoilRoot>
          <Counter></Counter>
          <Counter></Counter>
        </RecoilRoot>

    </div>
  );
}

About

helper to use react reducer function with a recoil atom

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published