GithubHelp home page GithubHelp logo

ptzagk / react-immer-hooks Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sin/react-immer-hooks

0.0 1.0 0.0 21 KB

Easy immutability in React Hooks with Immer.

Home Page: https://www.npmjs.com/package/react-immer-hooks

License: MIT License

JavaScript 100.00%

react-immer-hooks's Introduction

React Immer Hooks

Easy immutability in React Hooks with Immer.

Note: React Hooks are currently a RFC proposal which may be subject to change. You'll need at least [email protected] to use this feature.

Installation

yarn add react-immer-hooks

Usage

useImmerState(initialState)
import { useImmerState } from 'react-immer-hooks'

const initialState = {
  clicks: 0,
  doubleClicks: 0
}

const ClickCounters = () => {
  const [ state, setState ] = useImmerState(initialState)

  const onClick = () => setState(draft => { draft.clicks++ })
  const onDoubleClick = () => setState(draft => { draft.doubleClicks++ })

  return (
    <>
      <button onClick={onClick} onDoubleClick={onDoubleClick}>
        Clics: {state.clicks}, Double clicks: {state.doubleClicks}
      </button>
    </>
  )
}
useImmerReducer(reducer, initialState)
import { useImmerReducer } from 'react-immer-hooks'

const initialState = {
  count: 0
}

const reducer = (draft, action) => {
  if (action.type === 'INCREMENT') draft.count++
  if (action.type === 'DECREMENT') draft.count--
  if (action.type === 'ADD') draft.count += action.payload
}

const Counter = () => {
  const [ state, dispatch ] = useImmerReducer(reducer, initialState)

  return (
    <>
      Count: {state.count}
      <button onClick={() => dispatch({ type: 'INCREMENT'})}>
        Increment
      </button>
      <button onClick={() => dispatch({ type: 'DECREMENT'})}>
        Decrement
      </button>
      <button onClick={() => dispatch({ type: 'ADD', payload: 5})}>
        Add 5
      </button>
    </>
  )
}

License

MIT License

react-immer-hooks's People

Contributors

sin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.