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

tiny-react-redux

v0.0.11

Published

A tiny state management like redux and use them like redux toolkit

Downloads

13

Readme

Tiny React Redux

GitHub Workflow Status npm version npm downloads

The simple store like redux + react-redux + @redux/toolkit but smaller

Installation

Tiny React Redux is available as a package on NPM for use with a module bundler or in a Node application:

NPM

npm install tiny-react-redux

Yarn

yarn add tiny-react-redux

Purpose

The Tiny React Redux package is a package like redux. But it allow you using them like react redux and @redux/toolkit.

What's Included

Tiny React Redux includes these APIs:

  • createStore(): It can automatically combine your slice reducers.
  • createSlice(): Accepts an object of reducer functions, a slice name, and an initial state value, and automatically generates a slice reducer with corresponding action creators and action types

How to use

Create file store/cart.ts:

import { toast } from "react-toastify";
import axios from "axios";
import { store } from ".";
import { createSlice, PayloadAction } from "tiny-react-redux";
interface CartItem {
  id: string;
  name: string;
  quantity: number;
  price: number;
}
export type CartState = {
  miniCart: CartItem[];
};
const cartState: CartState = {
  miniCart: [],
};

const cartSlice = createSlice({
  name: "cart",
  initialState: cartState,
  reducers: {
    addToCart(state, action: PayloadAction<CartItem>) {
      state.miniCart = [...state.miniCart, action.payload];
    },
    // A common mistake is to try assigning state = someValue directly. This will not work!
    errorReplaceCart(state, { payload }: PayloadAction<CartState>) {
      state = payload;
    },
    //Instead, to replace the existing state, you should return the new value directly:
    correctReplaceState(state, { payload }: PayloadAction<CartState>) {
      return payload;
    },
  },
});
const { addToCart } = cartSlice.actions;
const CART_API = "api/cart";
export const addToCartToApi = async (cartItem: CartItem) => {
  try {
    const { data } = await axios.post<CartItem>(CART_API, { cartItem });
    store.dispatch(addToCart(data));
  } catch (error) {
    console.error(error);
  }
};
export default cartSlice;

Create file store/index.ts:

import {
  createStore,
  TypedUseAppSelector,
  useSelector,
} from "tiny-react-redux";
import cartSlice from "./cart";

const rootSlice = {
  cart: cartSlice,
};
export const store = createStore(rootSlice);
export const useAppSelector: TypedUseAppSelector<
  ReturnType<typeof store.getState>
> = useSelector;

and in the file App.tsx:

import { Provider as StoreProvider } from "tiny-react-redux";
function MyApp() {
  return (
    <StoreProvider store={store}>
      <Component />
    </StoreProvider>
  );
}
export default MyApp;

Server side rendering with Nextjs, in the file _app.tsx:

import type { AppProps } from "next/app";
function MyApp({ Component, pageProps }: AppProps) {
  const initState = useMemo(() => {
    const initialState = store.getInitialState();
    return {
      ...initialState,
      ...{
        cart: {
          miniCart: pageProps.miniCart || initialState.cart.miniCart,
        },
      },
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  return (
    <StoreProvider initialStateProvider={initState} store={store}>
      <Component {...pageProps} />
    </StoreProvider>
  );
}

export default MyApp;

in the component;

import { useAppSelector } from "src/store";
import { useDispatch } from "tiny-react-redux";
import { addToCartToApi, addToCart } from "src/store/cart";
const Component = () => {
  const miniCart = useAppSelector((state) => state.cart.miniCart);
  const dispatch = useDispatch();
  const onDispatchMiniCart = () => {
    dispatch(addToCart(/*cartItem*/));
  };
  const callApiAndDispatch = () => {
    addToCartToApi(/*cartItem*/);
  };
  return <div>{miniCart.map((item) => item.name)}</div>;
};