Giter Site home page Giter Site logo

alradadi / create-shared-state Goto Github PK

View Code? Open in Web Editor NEW
10.0 1.0 0.0 33 KB

A react hook that is exactly like useState but state is shared instead

License: MIT License

JavaScript 3.61% TypeScript 96.39%
react hook state shared global usestate

create-shared-state's Introduction

create-shared-state

A hook that has the exact same API as useState but the state can be shared between components instead.

Motivation

Most of the state management tools out there force you to create and initialize your shared state outside components and custom hooks. Well, sometimes the initial state isn't available until the component calls another hook. The hook provided by create-shared-state can be used exactly like the default useState except that state is shared between components.

Installation

yarn add create-shared-state

Usage

  • Example 1
import { create } from 'create-shared-state';

const useSharedState = create();

const useCounter = () => useSharedState(0); // the custom hook useCounter can be used in any component

const [counter, setCounter] = useCounter() // use in components

  • Example 2
import { create } from 'create-shared-state';
import { useForm } from 'react-hook-form';


const useSharedState = create();

/**
* The logic for this form and its methods is accessible from anywhere.
* This makes it possible for one component to handle updating and validating the form,
* and another component to handle the submission for examaple. 
*/
const useOnboardingForm = () => {
  const methods = useForm()
  
  const [sharedMethods] = useSharedState(methods)

  return sharedMethods
}

License

MIT

create-shared-state's People

Stargazers

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