Giter VIP home page Giter VIP logo

react-nano-store's Introduction

react-hot-toast - Try it out

Incredibly lightweight 0.5kb, blazing fast, easy to use state management utility


  • React Nano Store is designed to prevent unnecessary component re-renders by only re-rendering components if their dependent value has changed.
  • It requires no boilerplate code, making it very easy to create a store and get started.
  • No need to write any types for the store, as the hook will be type-safe by default.
  • No need to wrap your app/page/compoent in Provider, it just works.

React Nano Store provides solutions to many of the issues commonly associated with using React.Context, such as unnecessary re-renders the need for boilerplate code, and difficulty of use.

Nano Store and Context Comparison

Installation

With yarn

yarn add react-nano-store

With NPM

npm install react-nano-store

Getting Started

You can create a store anywhere in your app and use the hooks returned by it to ensure that you have access to the store wherever you use the hook.

import { createStore } from 'react-nano-store';

const initialStoreValue = {name: 'Baby Yoda', age: 50 }

const  useStore = createStore(initialStoreValue);

const ComponentOne = () => {
  //hook takes an array of string, which tells it what values to get from store
  const [{ age }, updateStore] = useStore(["age"]);
  return <button onClick={() => updateStore({ age: age + 1 })}>{age}</button>;
};

const ComponentTwo = () => {
  const [{ age }] = useStore(["age"]);
  return (
    <div>
      {/* age here will automatically get updated when changed from ComponentOne */}
      {age}
    </div>
  );
};

API

The react-nano-store library returns a function that creates a store. This function takes an initial store value and returns a hook that can be used in any component to access and update the store value.

import { createStore } from 'react-nano-store';
// you can name this hook anything
const initialStore = {name: 'Baby Yoda', age: 50 };

const  useStore = createStore(initialStore);

Hook

The hook returned by the createStore function takes an array of strings (representing the keys of the store object) as an argument. It returns an array containing two items:

  1. The first item is an object that consists of the values for the keys passed to the hook.
  2. The second item is a store update function that can update the values in the store. This function will only update the values for the keys that were passed as arguments to the hook. It is not possible to pass any other data to the update function.
const initialStore = {name: 'Baby Yoda', age: 50 };

const  useStore = createStore(initialStore);

const [{name}, updateState] = useStore(['name'])

Nano Store VS Context

Nano Store

import { createStore } from 'react-nano-store';

const useStore = createStore({count: 0});

const ComponentOne = () => {
  const [{ count }, updateStore] = useStore(["count"]);

  return <button onClick={() => updateStore({ count: count + 1 })}>{count}</button>;
};

Context

import { useContext, useState } from "react";

const context = React.createContext<ContextType>({
  count: 1,
  updateCount: (newCount: number) => {},
});

const Provider = ({ children }: any) => {
  const [count, setCount] = useState(0);

  const updateCount = (newCount: number) => {
    setCount(newCount);
  };

  return (
    <context.Provider value={{ count, updateCount }}>
      {children}
    </context.Provider>
  );
};

const Component = () => {
  const { count, updateCount } = useContext(context);

  return <button onClick={() => updateCount(count + 1)}>{count}</button>;
};

const App = () => {
  return (
    <Provider>
      <Component />
    </Provider>
  );
};

react-nano-store's People

Contributors

aadilhasan avatar

Stargazers

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