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

native-state-react

v2.0.1

Published

Very lightweight and most efficient implementation for react global state,just using react hooks. Magic !

Downloads

11

Readme

Native Global state for React Node.js Package

A native global state implementation with react. At least React version 18.2.0 is required.

Lightweight and most efficient implementation for react global state,just using in built react hooks. Magic !

Component renders only if the slice taken(in example s.name) changes.

No external dependencies used.

Very lightweight (605 bytes in size)

Simple usage( no reducers, no actions and no boilerplate code required )

Perfect replacement for Redux and Mobx

First, import and Add <Root /> as a component in the top level.

then, import and use useSelector to get the desired global state slice.

const [name,setState] = useSelector(s=>s.name);

if name not found in global state, it will return undefined.

And you can update the name like this..

setState({name:"Will"});

Add/update another state property by

setState({school:{class:"VII"}});

See example folder for react project example implementation.

Full implementation

Add <Root> in your top component tree (index.js),

import React from 'react';
import ReactDOM from 'react-dom';
//Add import 
import { Root } from 'native-state-react'; 

let store={ 
    name:"Mary",
    school:{class:"V"}
};
ReactDOM.render(
 <React.StrictMode>
  <Root initial={store} /> // 'initial' prop is optional(default will be empty object).
  <App/>
 </React.StrictMode>,
document.getElementById('root'),
)

In your component

import { useSelector } from 'native-state-react';
function App() {
    const [name,setState] = useSelector(s=>s.name);
    return <div>{name}</div>
}

Update name in the state like this

setState({name:"George"});

it just replaces the given name in the existing state, other state values will stay unchanged. the school object will stay unchanged.

Example with state update.

import { useSelector } from 'native-state-react';
function Class() {
    const [name,setState] = useSelector(s=>s.name);
    useEffect(()=>{
	    setTimeout(() => {
		    setSt({name:"George"});
	    }, 3000);
	 },[]);
    
    return <div>{name}</div>
}

With Above code, you can see the name gets updated in UI after 3 seconds.