Simplest React Store

Simplest react store that does the job.
This package was originally written for fun some years ago.
Lightweight and easy-to-use state management solution for React applications. It provides a simple API for creating and managing stores, making state management more approachable and less cumbersome. With this package, developers can quickly set up and utilize state management in their React projects without the need for complex setups or extensive boilerplate code.
The key features of Simplest-React-Store include:
-
Minimalist Approach: The package embraces simplicity and aims to provide a straightforward solution for state management in React applications.
-
Small Footprint: Simplest-React-Store is designed to be lightweight and has a minimal impact on bundle size, ensuring optimal performance for applications.
-
Intuitive API: The API is intuitive and easy to understand, making it accessible for developers of all skill levels.
Table of Contents
Installation

Using npm:
npm install simplest-react-store
Using yarn:
yarn add simplest-react-store
Usage
1. Define your store
import { createStore } from 'simplest-react-store';
const initialState = {
isHelloShown: false,
helloText: "",
user: {
name: "Matt",
age: 40,
},
};
export type State = typeof initialState;
const actions = {
showHello: (state: State, toggle: boolean) => (state.isHelloShown = toggle),
setHelloText: (state: State, value: string) => (state.helloText = value),
setUser(
state: State,
newUserData: Partial<State["user"]>
) {
return { user: { ...state.user, ...newUserData } };
},
};
export const { useStore, useStoreProp, Provider } = createStore(
initialState,
actions
);
2. Import Provider in your main app file
Wrap your React application with the Provider component provided by the store. This makes the store available to all components in the application.
import { Provider as MyStoreProvider } from "./store";
function App({ Component, pageProps }: AppProps) {
return (
<MyStoreProvider>
/* Your app code */
</MyStoreProvider>
);
}
3. Use state anywhere in your components
Access the store's state and actions in your components using the useStore and useStoreProp hooks.
import { useEffect } from 'react';
import { useStoreProp } from "store";
export default function MyComponent() {
const [helloText] = useStoreProp("helloText");
const [user] = useStoreProp("user");
const [showHello, dispatch] = useStoreProp("isHelloShown");
useEffect(() => {
dispatch.setUser({name: "David", age: 19});
dispatch.setHelloText("Hello");
dispatch.showHello(true);
}, [dispatch]);
return (
<div>
{showHello && (
<span>
{helloText} {user.name}
</span>
)}
</div>
);
}
That's it! Now you can use the state properties and actions from the store in your components to manage and update the state of your application. Remember to import the necessary functions and components from the store.ts file where needed.
Support and collaboration
If you have any idea for an improvement, please file an issue. Feel free to make a PR if you are willing to collaborate on the project. Thank you :)