Giter Site home page Giter Site logo

myckhel / use-react-state Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 1.49 MB

react useState is now more intelligent

Home Page: https://github.com/myckhel/use-react-state#use-react-state

HTML 25.45% JavaScript 68.61% CSS 5.94%
react reactjs use-state react-use-state react-state functional-components

use-react-state's Introduction

use-react-state

managing state in react is now more intelligent.

NPM JavaScript Style Guide

Overview

use-react-state is an advanced implementation of React's useState It gives almost the same and more functionality as React's useState.

Using Reacts useState

😒😭😿

import { useState } from 'react'

function Component(){
  const [first, setFirst] = useState(1)
  const [second, setSecond] = useState(2)
  const [third, setThird] = useState(3)
  const [fourth, setFourth] = useState(4)

  const changeFirstAndSecond = () => {
    setFirst(10)
    setSecond(20)
    // 2x re-render
  }

  const resetState = () => {
    setFirst(1)
    setSecond(2)
    setThird(3)
    setFourth(4)
    // 4x re-render
  }
}

Using use-react-state as useState replacement.

πŸ˜ŠπŸ˜ƒπŸ˜ΊπŸ˜„

import useState from 'use-react-state'

function Component(){
  const [state, setState] = useState({
    first: 1,
    second: 2,
    third: 3,
    fourth: 4,
  })

  const changeFirstAndSecond = () => {
    setState({
      first: 10,
      second: 20,
    })
    // 1x re-render
  }

  const resetState = () => {
    setState({
      first: 1,
      second: 2,
      third: 3,
      fourth: 4,
    })
    // 1x re-render
  }
}

Install

yarn add use-react-state

Usage

import React from 'react'

import useState from 'use-react-state'

const App = () => {
  const [state, setState] = useState({ nine: 9 })

  return (
    <div>
      <p>Nine: is {state.nine}</p>

      <button onClick={() => setState(({ nine }) => ({ nine: nine + 1 }))}>
        Increaae state.nine by 1
      </button>
    </div>
  )
}

APIs

useState

is a Hook that lets you add React state to function components.

@params

  • name description type
    initialState? state's initial state any

@return

  • type array
    • state | current state value | any
    • setState | function to update the state | function
    • stateRef | state reference | object
import useState from 'use-react-state'
const [state, setState, stateRef] = useState(initialState)

setState

function to update the state. setState is an intelligent setter function which guesses how you intends to update an existing state. When array is passed as the new state, it assumes you will append the newState array with the current state if current state is an array else it replaces the state. same goes for objects.

@params

  • name description type
    newState|callback new state to set or setter callback any

using setState

Given the 1st example below, setState will merge new state object with existing state given that existing state is an object.

import useState from 'use-react-state'

const [state, setState, stateRef] = useState({ foo: 'foo' })

// merge state object
setState({ bar: 'bar' }) // {foo: 'foo', bar: 'bar'}

// replace state
setState('foo') // 'foo'

// replace state
setState(['foo']) // ['foo']

// push state array
setState(['bar']) // ['foo', 'bar']

// custom state setter
setState((existingDraftState) => {

  return ['baz']

}) // ['baz']

using setState's immer produce feature

setState uses immer's produce feature for state mutation. Your are not limited to using the functionality as well.

import useState from 'use-react-state'

const [state, setState] = useState({ foo: 'foo' })

// custom state setter with immer feature
setState((existingDraftState) => {

  existingDraftState.bar = 'bar'

}) // {foo: 'foo', bar: 'bar'}

License

MIT Β© myckhel

use-react-state's People

Contributors

myckhel avatar

Stargazers

 avatar

Watchers

 avatar  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.