1.1.6 โ€ข Published 9 months ago

react-state-shed v1.1.6

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

๐ŸŒธ React State Shed

https://github.com/Leeseunghwan7305/react-state-shed

๐ŸŒธ ์„ค์น˜


npm i react-state-shed

๐ŸŒธ ์‚ฌ์šฉ๋ฒ•

๐ŸŒผ Shed๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

export const counterShed =
  createShed <
  ShedState >
  ("count",
  {
    count: 0,
    count2: 0,
    count3: 0,
    increment: (key) => {
      counterShed.setState((prev) => ({ [key]: prev[key] + 1 }));
    },
    decrement: (key) => {
      counterShed.setState((prev) => ({ [key]: prev[key] - 1 }));
    },
  });

Shed๋Š” ๊ธฐ๋ณธ์ ์ธ ์ „์—ญ ์ƒํƒœ ๋‹จ์œ„์ž…๋‹ˆ๋‹ค. createShed ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ „์—ญ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋Š” useShed ํ›…์„ ํ†ตํ•ด ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐ŸŒผ useShed๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

const { count, increment, decrement } =
  useShed < ShedState > ("count", (state) => state);

์ด๋•Œ ์ˆ˜๋™ ๋ Œ๋”๋ง ์ตœ์ ํ™”๋ฅผ ์ ์šฉํ•˜๋ฉด ๋ถˆํ•„์š”ํ•œ ๋ฆฌ๋ Œ๋”๋ง์„ ๋ง‰์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐ŸŒผ Shed๋ฅผ ์‚ฌ์šฉํ•ด ์ „์—ญ์œผ๋กœ Count๋ฅผ ์˜ฌ๋ฆฌ๊ณ  ๋‚ด๋ฆฌ๋Š” ์˜ˆ์ œ

Shed ์ƒํƒœ ํƒ€์ž… ์ •์˜ ๋ฐ ์ƒ์„ฑ

export type ShedState = {
  count: number;
  count2: number;
  count3: number;
  increment: (
  key: keyof Pick<ShedState, "count" | "count2" | "count3">
  ) => void;
  decrement: (
  key: keyof Pick<ShedState, "count" | "count2" | "count3">
  ) => void;
};

export const counterShed = createShed<ShedState>("count", {
  count: 0,
  count2: 0,
  count3: 0,
  increment: (key) => {
  counterShed.setState((prev) => ({ [key]: prev[key] + 1 }));
  },
  decrement: (key) => {
  counterShed.setState((prev) => ({ [key]: prev[key] - 1 }));
  },
});

์•ฑ ์ปดํฌ๋„ŒํŠธ

function App() {
  return (
    <div>
      <One />
      <Two />
      <Three />
    </div>
  );
}

One ์ปดํฌ๋„ŒํŠธ

const One = () => {
  const { count, increment, decrement } =
    useShed < ShedState > ("count", (state) => state);
  return (
    <div style={{ height: "100px", backgroundColor: "green" }}>
      ์นด์šดํŠธ 1 : {count}
      <button onClick={() => increment("count")}>count1 ์˜ฌ๋ฆฌ๊ธฐ</button>
      <button onClick={() => decrement("count")}>count1 ๋‚ด๋ฆฌ๊ธฐ</button>
    </div>
  );
};

export default One;

Two ์ปดํฌ๋„ŒํŠธ

const Two = () => {
  const count = useShed < ShedState > ("count", (state) => state.count2);
  return (
    <div style={{ height: "100px", backgroundColor: "blue" }}>
      ์นด์šดํŠธ 2 : {count}
    </div>
  );
};

export default Two;

Three ์ปดํฌ๋„ŒํŠธ

const Three = () => {
  const count = useShed < ShedState > ("count", (state) => state.count3);
  return (
    <div style={{ height: "100px", backgroundColor: "red" }}>
      ์นด์šดํŠธ 3 : {count}
    </div>
  );
};
export default Three;

๐ŸŒธ ์‹คํ–‰ ๊ฒฐ๊ณผ

์‹คํ–‰ ๊ฒฐ๊ณผ

1.1.6

9 months ago

1.1.5

10 months ago

1.1.4

10 months ago

1.1.3

10 months ago

1.1.2

10 months ago

1.1.1

10 months ago

1.1.0

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago