Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
react-nano-store
Advanced tools
react-nano-store is an incredibly lightweight 0.5kb, blazing fast, easy to use state management utility for React.
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
yarn add react-nano-store
npm install react-nano-store
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>
);
};
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);
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:
const initialStore = {name: 'Baby Yoda', age: 50 };
const useStore = createStore(initialStore);
const [{name}, updateState] = useStore(['name'])
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>;
};
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>
);
};
FAQs
react-nano-store is an incredibly lightweight 0.5kb, blazing fast, easy to use state management utility for React.
The npm package react-nano-store receives a total of 4 weekly downloads. As such, react-nano-store popularity was classified as not popular.
We found that react-nano-store demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.