Skip to content

Commit 57d9f4d

Browse files
Reboot:9
1 parent c9f2406 commit 57d9f4d

11 files changed

Lines changed: 526 additions & 189 deletions

File tree

UI/SPA/React/vite-redux-ts/package-lock.json

Lines changed: 206 additions & 114 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UI/SPA/React/vite-redux-ts/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
"preview": "vite preview"
1111
},
1212
"dependencies": {
13+
"@reduxjs/toolkit": "^2.11.2",
1314
"@tailwindcss/forms": "^0.5.11",
1415
"react": "^19.2.4",
1516
"react-dom": "^19.2.4",
17+
"react-redux": "^9.2.0",
1618
"react-router-dom": "^7.13.2",
1719
"tailwindcss": "^4.2.2"
1820
},

UI/SPA/React/vite-redux-ts/src/App.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import About from './pages/About'
77
import Settings from './pages/Settings'
88
import { Counter } from './pages/Counter'
99
import { FetchData } from './pages/FetchData'
10-
import { CrudSample } from './pages/CrudSample'
11-
import { CrudSample2 } from './pages/CrudSample2'
10+
// 名前付きimport から default import に変更 connectされたComponentを使用)
11+
import CrudSample from './pages/CrudSample'
12+
import CrudSample2 from './pages/CrudSample2'
1213
import { RedirectOfAuth } from './components/RedirectOfAuth';
1314

1415
import './App.css'

UI/SPA/React/vite-redux-ts/src/main.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@ import { createRoot } from 'react-dom/client'
33
import './index.css'
44
import App from './App.tsx'
55

6+
// Redux 関連のインポート
7+
import { Provider } from 'react-redux'
8+
import { store } from './store'
9+
10+
611
createRoot(document.getElementById('root')!).render(
712
<StrictMode>
8-
<App />
13+
<Provider store={store}>
14+
<App />
15+
</Provider>
916
</StrictMode>,
1017
)
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
import { useSelector } from 'react-redux'
2+
import type { RootState } from '../store'
3+
14
export default function About() {
2-
return <div><h1>About</h1><p>About Pageです。</p></div>
5+
// Reduxのstoreから値を取得
6+
const count = useSelector((state: RootState) => state.counter.value)
7+
const message = useSelector((state: RootState) => state.crudSample.message)
8+
9+
return <div>
10+
<h1>About</h1>
11+
<p>About Pageです。</p>
12+
{/* countを表示 */}
13+
<p>現在のカウント: {count}</p>
14+
{/* messageを表示 */}
15+
<p>現在のメッセージ: {message}</p>
16+
</div>
317
}
Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,71 @@
11
import * as React from 'react';
22

3-
// Propsの型定義(今回は不要だが明示)
4-
type CounterProps = {};
3+
// Redux 関連のインポート
4+
import { useSelector, useDispatch } from 'react-redux';
5+
import type { RootState, AppDispatch } from '../store';
6+
import { increment, reset } from '../store/counterSlice';
7+
8+
// --- 既存コード
9+
10+
// Propsの型定義
11+
type CounterProps //= {};
12+
= {
13+
currentCount: number;
14+
onIncrement: () => void;
15+
onReset: () => void;
16+
};
517

618
// Stateの型定義
719
type CounterState = {
820
currentCount: number;
921
};
1022

11-
export class Counter extends React.Component<CounterProps, CounterState> {
12-
constructor(props: CounterProps) { // ← props を受け取るよう修正
13-
super(props); // ← super(props) に修正
23+
// 以下、Redux修正あり
24+
// クラス名称を変更
25+
export class Counter_org extends React.Component<CounterProps, CounterState> {
26+
constructor(props: CounterProps) {
27+
super(props);
1428
this.state = { currentCount: 0 };
1529
}
1630

31+
// Reduxのdispatchを呼び出す形に変更
1732
render() {
1833
return (
1934
<div>
2035
<h1>Counter</h1>
2136
<p>This is a simple example of a React component.</p>
22-
<p>Current count: <strong>{this.state.currentCount}</strong></p>
23-
<button className='btn-primary' onClick={() => this.incrementCounter()}>
37+
<p>Current count: <strong>{this.props.currentCount}</strong></p>
38+
<button className='btn-primary' onClick={() => this.props.onIncrement()}>
2439
Increment
2540
</button>
41+
<button className='btn-primary' onClick={() => this.props.onReset()}>
42+
Reset
43+
</button>
2644
</div>
2745
);
2846
}
2947

30-
incrementCounter() {
48+
/*incrementCounter() {
3149
this.setState({
3250
currentCount: this.state.currentCount + 1,
3351
});
34-
}
52+
}*/
3553
}
54+
55+
// --- Reduxラッパー ---
56+
export const Counter = () => {
57+
const currentCount = useSelector((state: RootState) => state.counter.value);
58+
const dispatch = useDispatch<AppDispatch>();
59+
60+
// dispatch の戻り値を void に吸収させる
61+
const handleIncrement = (): void => { dispatch(increment()); };
62+
const handleReset = (): void => { dispatch(reset()); };
63+
64+
return (
65+
<Counter_org
66+
currentCount={currentCount}
67+
onIncrement={handleIncrement}
68+
onReset={handleReset}
69+
/>
70+
);
71+
};

0 commit comments

Comments
 (0)