Giter Site home page Giter Site logo

use-object-state-npm's Introduction

@rehmat-falcon/use-object-state

This repository houses the implementation of useObjectState hook.

useObjectState is a custom react hook developed to mimic the behavior of the old setState method while using class components.

This hook uses useState hook under the hood. In fact, it is just a wrapper around it to ensure that we dont have to perform destructuring operation when dealing with object state and also to ensure that we dont intruduce a data reset bug by accident; happens when we setState without specifying other values when using react hook useState

The implementation is simple and given below :

import { useState } from "react";

const useObjectState = (initial = {}) => {
  if(typeof initial != "object") {
    throw new Error("useObjectState can only be used with objects");
  }
  const [state, updateState] = useState(initial);

  const setState = (newStateValue) => {
    updateState({
      ...state,
      ...newStateValue
    });
  }

  return [
    state,
    setState
  ];
};

export default useObjectState;

You can use the above hook in your react components.

  const [state, updateState] = useObjectState({
    name : "Name",
    unchangedValue : 12
  });
  const updateName = () => updateState({
    name : "New updated name"
  });
  // updateName method just updates the name. We dont have to care about the other values in the object here.
  // without it, we would have to use something like Object.assign({}, state, {name : "new name"}); or setState({...state, {name:"New name"}})
  // Our custom hook uses the latter approach under the hood.

MIT © RehmatFalcon


This hook is created using create-react-hook.

use-object-state-npm's People

Contributors

niroula-kushal avatar

Stargazers

Sujan Rai avatar

Watchers

James Cloos 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.