Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comparison of the amount of code with and without using constate #4

Merged
merged 1 commit into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions react-context-constate-sandbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^13.0.0",
"@testing-library/user-event": "^13.2.1",
"constate": "^3.3.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
Expand Down
35 changes: 9 additions & 26 deletions react-context-constate-sandbox/src/DivideContextWithConstate.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { createContext, useCallback, useContext, useState } from "react";

// Context を、参照が変更する count用 と 参照が変わらない increment 関数用の2つ作成する
const CountContext = createContext({});
const IncrementContext = createContext({});
import constate from "constate";
import { useCallback, useState } from "react";

// countと、increment を定義して返却するカスタムフックス
function useCounter({ initialCount = 0 } = {}) {
Expand All @@ -11,25 +8,11 @@ function useCounter({ initialCount = 0 } = {}) {
return { count, increment };
}

// 2つのプロバイダーをネストして1つのプロバイダーにする
const CounterProvider = ({ children, initialCount }) => {
const { count, increment } = useCounter({ initialCount });
return (
<CountContext.Provider value={count}>
<IncrementContext.Provider value={increment}>
{children}
</IncrementContext.Provider>
</CountContext.Provider>
);
};

const useCount = () => {
return useContext(CountContext);
};

const useIncrement = () => {
return useContext(IncrementContext);
};
const [CounterProvider, useCount, useIncrement] = constate(
useCounter,
(value) => value.count,
(value) => value.increment
);

// ボタンコンポーネント
function IncrementButton() {
Expand All @@ -45,7 +28,7 @@ function Count() {
return <span>{count}</span>;
}

function DivideContext() {
function DivideContextWithConstate() {
return (
<CounterProvider initialCount={1}>
<Count />
Expand All @@ -54,4 +37,4 @@ function DivideContext() {
);
}

export default DivideContext;
export default DivideContextWithConstate;
5 changes: 5 additions & 0 deletions react-context-constate-sandbox/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import ReactDOM from "react-dom/client";
import DivideContext from "./DivideContext";
import DivideContextWithConstate from "./DivideContextWithConstate";
import OneContext from "./OneContext";
import OneContextWithReducer from "./OneContextWithReducer";

Expand All @@ -21,5 +22,9 @@ root.render(
useState(ボタンをクリックすると、1つのコンポーネントのみレンダリングされる)
</h2>
<DivideContext />
<h2>
constateを使用(ボタンをクリックすると、1つのコンポーネントのみレンダリングされる)
</h2>
<DivideContextWithConstate />
</React.StrictMode>
);
5 changes: 5 additions & 0 deletions react-context-constate-sandbox/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3178,6 +3178,11 @@ connect-history-api-fallback@^2.0.0:
resolved "https://registry.yarnpkg.com/connect-history-api-fallback/-/connect-history-api-fallback-2.0.0.tgz#647264845251a0daf25b97ce87834cace0f5f1c8"
integrity sha512-U73+6lQFmfiNPrYbXqr6kZ1i1wiRqXnp2nhMsINseWXO8lDau0LGEffJ8kQi4EjLZympVgRdvqjAgiZ1tgzDDA==

constate@^3.3.2:
version "3.3.2"
resolved "https://registry.yarnpkg.com/constate/-/constate-3.3.2.tgz#a6cd2f3c203da2cb863f47d22a330b833936c449"
integrity sha512-ZnEWiwU6QUTil41D5EGpA7pbqAPGvnR9kBjko8DzVIxpC60mdNKrP568tT5WLJPAxAOtJqJw60+h79ot/Uz1+Q==

content-disposition@0.5.4:
version "0.5.4"
resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.4.tgz#8b82b4efac82512a02bb0b1dcec9d2c5e8eb5bfe"
Expand Down