Skip to content

Single dependency (react) global state. Simplicity πŸ‘Œ meets performance πŸ”₯

Notifications You must be signed in to change notification settings

kasperpihl/react-slice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

React Slice

A simple performant approach to state management in React.

  1. Uses reducers and dispatch πŸ€–
  2. Access state from anywhere with hooks πŸš€
  3. You can still use statically 😲
  4. Optimized for use with Typescript πŸ’™
  5. Full control of render performance πŸ”₯
  6. 2kb minified πŸ’ͺ

Demo

I've made a few Codesandboxes to play around with react-slice

Installation

npm i react-slice

The basics

Create a file and export your hook w/ reducer (fx counterSlice.js)

// counterSlice.js
import { createSlice } from 'react-slice'; // πŸ‘ˆπŸ‘ˆπŸ‘ˆ

export default createSlice({
  reducer: /*πŸ‘ˆπŸ‘ˆπŸ‘ˆ*/ (state, action) => {
    switch (action.type) {
      case 'increment':
        return state + 1;
      default:
        return state;
    }
  },
  initialState: /*πŸ‘ˆπŸ‘ˆπŸ‘ˆ*/ 0,
  debugName: 'Counter', // Optional.
});

Then import that file somewhere else (fx App.jsx) πŸ΄β€β˜ οΈ

// App.jsx
import React from 'react';
import counterSlice from './counterSlice'; // πŸ‘ˆπŸ‘ˆπŸ‘ˆ

export default function App(test) {
  const counterState = counterSlice.use(); // πŸ‘ˆπŸ‘ˆπŸ‘ˆ

  const onClick = () => {
    counterSlice.dispatch({ type: 'increment' }); // πŸ‘ˆπŸ‘ˆπŸ‘ˆ
  };
  return (
    <div className="App">
      <h1>{counter /*πŸ‘ˆπŸ‘ˆπŸ‘ˆ*/}</h1>
      <button onClick={onClick}>Increment</button>
    </div>
  );
}

And boom! πŸ’₯ That's all you need to get up and running!

Advanced/FAQ

About

Single dependency (react) global state. Simplicity πŸ‘Œ meets performance πŸ”₯

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published