npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-nano-store

v0.1.3

Published

react-nano-store is an incredibly lightweight 0.5kb, blazing fast, easy to use state management utility for React.

Downloads

154

Readme

  • 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>
  );
};