New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

react-use-slice

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-use-slice

A react hook to use reducers with an API like createSlice from Redux toolkit, in a typesafe way, with performance in mind.

latest
Source
npmnpm
Version
1.0.4
Version published
Maintainers
1
Created
Source

react-use-slice

A react hook to use reducers with an API like createSlice from Redux toolkit, in a typesafe way, with performance in mind.

Install

npm install react-use-slice

Example

JavaScript

import { createContext } from 'react';
import useSlice from 'react-use-slice';

const initialState = {
  score: 0
};

const scoreSlice = {
  name: 'score', // "name" is optional. It is used in dev tools
  initialState,
  reducers: {
    increment(state, payload) {
      const score = state.score + 1;
      return { ...state, score };
    },
    decrement(state, payload) {
      const score = state.score - 1;
      return { ...state, score };
    }
  }
};

export const ScoreContext = createContext(null);

export const ScoreProvider = ({ children }) => {
  // Actions will be automaticaly dispatched
  const [state, actions] = useSlice(scoreSlice);

  return (
    <ScoreContext.Provider value={{ ...state, ...actions }}>
      {children}
    </ScoreContext.Provider>
  );
};

TypeScript

import { createContext } from 'react';
import useSlice, { createSlice, CombineStateAndActions } from 'react-use-slice';

const initialState = {
  score: 0
};

const scoreSlice = createSlice({
  name: 'score', // "name" is optional. It is used in dev tools
  initialState,
  reducers: {
    increment(state, payload: number) {
      const score = state.score + 1;
      return { ...state, score };
    },
    decrement(state, payload: number) {
      const score = state.score - 1;
      return { ...state, score };
    }
  }
});

export const ScoreContext =
  createContext<CombineStateAndActions<typeof scoreSlice> | null>(null);

export const ScoreProvider = ({ children }) => {
  // Actions will be automaticaly dispatched
  const [state, actions] = useSlice(scoreSlice);

  return (
    <ScoreContext.Provider value={{ ...state, ...actions }}>
      {children}
    </ScoreContext.Provider>
  );
};

Keywords

react

FAQs

Package last updated on 09 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts