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

simplest-react-store

v1.0.11

Published

[npm-url]: https://npmjs.org/package/simplest-react-store [npm-image]: http://img.shields.io/npm/v/simplest-react-store.svg

Downloads

31

Readme

Simplest React Store

NPM version Blazing Fast Code Coverage npm downloads install size

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:

  1. Minimalist Approach: The package embraces simplicity and aims to provide a straightforward solution for state management in React applications.

  2. Small Footprint: Simplest-React-Store is designed to be lightweight and has a minimal impact on bundle size, ensuring optimal performance for applications.

  3. Intuitive API: The API is intuitive and easy to understand, making it accessible for developers of all skill levels.

Table of Contents

Installation

NPM

Using npm:

npm install simplest-react-store

Using yarn:

yarn add simplest-react-store

Usage

1. Define your store

// store.ts
import { createStore } from 'simplest-react-store';

// Define the initial state of the store. This includes the properties isHelloShown, helloText, and user with their initial values.
const initialState = {
    isHelloShown: false,
    helloText: "",
    user: {
        name: "Matt",
        age: 40,
    },
};

export type State = typeof initialState;

// Define the actions that can be performed on the store.
// These actions are functions that receive the current state and
// any necessary parameters, and they update the state accordingly.
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"]>
    ) {
        // For objects, it works similar to a standard reducer and is particularly useful for handling nested objects

        // Update the user state by merging newUserData with the current user object
        return { user: { ...state.user, ...newUserData } };
    },
};

// Create the store by calling the createStore function and passing the initial state and actions.
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.

// _app.tsx
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.

// MyComponent.tsx
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(() => {
        // You can use same dispatch for all actions as it is the same store
        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 :)