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

simple-reactjs-store

v0.0.20

Published

Simple react store using contextAPI

Downloads

11

Readme

Simple React Store

  • A library to implement global state in react.

  • In v0.0.18 of this library... rxjs has been implemented instead of contextApi, but you can still use the React Context DevTool as normal :)

-Cache in localstorage implemented

Old Documentations

Why?

On many occasions you have small project ideas, I have implemented a contextApi architecture, Redux, Zuztand, Etc, it takes a long time sometimes, Recoil is a solution but from my point of view I have had many problems using it...

Instalation

npm i simple-reactjs-store

React Context DevTool

You can use this extension to see all data of your store

Configuration

This library it's easy to configurate :)

1. Define your first store

  • You can create a new file in store/users.js (if you want create a beautiful folder structure)
import { CreateSlice } from 'simple-reactjs-store';

export const userSlice = CreateSlice({
  name: 'users',
  initialState: [],
  reducer(state, action) {
    //your actions
    switch (action.type) {
      case 'ADD_USER':
        return [...state, action.payload];
      default:
        return state;
    }
  },
});

Using Typescript

import { CreateSlice } from 'simple-reactjs-store';

export interface User {
  name: string;
  email: string;
  age: number;
}

export const userSlice = CreateSlice({
  name: 'users',
  initialState: [] as User[],
  reducer(state, action) {
    switch (action.type) {
      case 'ADD_USER':
        return [...state, action.payload];
      default:
        return state;
    }
  },
});

Full Example of slice with typescript

  • Cache in localstorage is Experimental.
import { CreateSlice } from "simple-reactjs-store";

export interface Product {
  name: string;
  description: string;
  price: number;
  image: string;
}

//The best practice is create a constants for your actions but you are free to do what you want
export const PRODUCT_ACTIONS = {
  ADD_PRODUCT: "ADD_PRODUCT",
  DELETE_PRODUCT: "DELETE_PRODUCT",
};

export const productStore = CreateSlice({
  name: "products",
  initialState: [] as Product[], //you can define the state you want
  config: { useLocalStorageCache: true }, //CACHE STORAGE (EXPERIMENTAL)
  reducer(state, action) {
    switch (action.type) {
      case PRODUCT_ACTIONS.ADD_PRODUCT:
        return [...state, action.payload];
      case PRODUCT_ACTIONS.DELETE_PRODUCT:
        return state.filter((product) => product.name !== action.payload);
      default:
        return state;
    }
  },
});

2. Define a GlobalProvider in your App.js

The store props receive an Array of Slices

import { SimpleStateProvider } from 'simple-reactjs-store';
import { productState } from 'store/product';
import { userState } from 'store/user';

export default function App({ Component, pageProps }) {
  return (
    <SimpleStateProvider store={[productState, userState]}>
      <Component {...pageProps} />
    </SimpleStateProvider>
  );
}

Store management

To start using your state, you just have to import the hook useSimpleStore and pass it the state you want to use.

const { data, dispatch, asyncDispatch } = useSimpleStore(productStore);
  • You can use data to access to all data of store
console.log(data);

data.map((product, i) => (
  <div key={i}>
    <p>{product.name}</p>
  </div>
));
  • You can use select to suscribe a value of store
  • I recomend subscribe in useEffect
//suscribe to value
const mySubscription = select('isLoading').subscribe((value) => {
  console.log(`value isLoading change to ${value}`);
});

//unsuscribe
mySubscription.unsubscribe();
  • You can use dispatch to execute acction and pass a payload;
dispatch({
  type: 'ADD_PRODUCT', //Type of your action definition in slice
  payload: {
    name: 'TV Smart',
    description: 'This product will be saved in store',
  },
});
  • You can use asyncDispatch to promises
await asyncDispatch(CATEGORY_ACTIONS.SET_CATEGORIES, getCategories);
  • If you want set a loader in your promise...
const loadCategories = async () => {
  //you can use your store or useState... this is only example
  dispatch({ type: CATEGORY_ACTIONS.SET_ISLOADING, payload: true });
  //the promise need return the payload
  await asyncDispatch(CATEGORY_ACTIONS.SET_CATEGORIES, getCategories);

  dispatch({ type: CATEGORY_ACTIONS.SET_ISLOADING, payload: false });
};

Basic Full Example

import { useSimpleStore } from 'simple-reactjs-store';
import { productStore } from '../stores/product';

const ProductList = () => {
  const { data, dispatch } = useSimpleStore(productStore);

  const addProduct = () => {
    dispatch({
      type: 'ADD_PRODUCT', //Type of your action definition in slice
      payload: {
        name: 'TV Smart',
        description: 'This product will be saved in store',
      },
    });
  };

  return (
    <div>
      <button onClick={addProduct}>Click to add product</button>

      {/* accesing to data */}
      {data.length === 0 ? (
        <p>no products avalible</p>
      ) : (
        data.map((product, i) => (
          <div key={i}>
            <p>{product.name}</p>
          </div>
        ))
      )}
    </div>
  );
};

export default ProductList;

Custom Use - (Another way to use)

make use of as follows if you don't want to use cache on localStorage and also don't want to use ContextDevTools

  • why should i do it like this? In this way we have more flexibility and management of the state

advantages

  1. own hooks
  2. full state control
  3. dont need use GlobalProvider of this library
  4. you can manage subscriptions as you like
  • disadvantages
  1. local storage cache not found you need set manualy
  2. Context devTools not work
  3. you spend more time
import { mySlice } from 'store/mySlice';

const { store, slice } = mySlice;

const [myState, setMyState] = useState(slice.initialstate);

//dispatching to store
const saveNewValue = store.dispatch({ type: 'ADD', payload: 'Hello world' });

useEffect(() => {
  //suscription to store
  const subs = store.subscribe((data) => {
    setMyState(data);
  });

  //unsubscribe when the component unmount
  return () => subs.unsubscribe();
}, []);