Giter Site home page Giter Site logo

pknu-wap / wap-state Goto Github PK

View Code? Open in Web Editor NEW
4.0 0.0 1.0 583 KB

WAP 리액트 상태 관리 라이브러리

License: MIT License

JavaScript 11.19% Shell 1.57% TypeScript 84.83% HTML 2.41%
global-state-management react state-management wap state pknu-wap wap-state

wap-state's Introduction

WAP State

wap-state은 Zustand의 영향을 받아 개발한 상태 관리 라이브러리입니다.

npm downloads Github Stars

Feature

  • Shallow Equal: 필요한 상태만 선택적으로 공유할 수 있고, 리렌더링을 방지하여 효율적인 상태 관리를 할 수 있습니다.
  • lightweight: 불필요한 코드를 모두 제거하여 경량화된 라이브러리입니다.
  • Support Typescript: 타입스크립트를 지원합니다.

Installation

Install this library with the following command.

$ pnpm add wap-state
# or
$ yarn add wap-state
# or
$ npm intall wap-state

Usage

First create a state

You can create global state management hook through wstate. Create states and actions types for the correct type, and create states and actions inside the wstate.

// src/stores/useCounterStore.ts

import { wstate } from 'wap-state';

type States = {
  count: number;
  step: number;
};

type Actions = {
  increment: (num: number) => void;
  decrement: (num: number) => void;
  resetCount: () => void;
};

const useCounterStore = wstate<States & Actions>((set) => ({
  count: 0,
  step: 0,
  increment: (num: number) => set((state) => ({ count: state.count + num })),
  decrement: (num: number) => set((state) => ({ count: state.count - num })),
  resetCount: () => set({ count: 0 }),
}));

export default useCounterStore;

Use the state or actions from your components

import useCounterStore from './stores/useCounterStore';

const CounterComponent = () => {
  const { count, increment, decrement, resetCount } = useCounterStore();

  return (
    <div>
      <div>{count}</div>
      <button onClick={() => increment(1)}>+</button>
      <button onClick={() => decrement(1)}>-</button>
      <button onClick={() => resetCount()}>reset</button>
      <div>{counteA}</div>
    </div>
  );
};

export default CounterComponent;

Select part of state

If you want to selet part of the state, you can pass selector function as argument. If you select multiple fields, the component will rerender after shallow comparison.

const { count, step } = useCounterStore((state) => ({
  count: state.count,
  step: state.step,
}));

Or you can use custom equality function.

const count = useCounterStore(
  (state) => ({ count: state.count, step: state.step }),
  // this part
  (prev, new) => customCompare(prev, new),
);

wap-state's People

Contributors

alstn113 avatar

Stargazers

 avatar  avatar  avatar  avatar

Forkers

alstn113

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.