Skip to content

Commit a48a743

Browse files
committed
サンプルアプリケーションを作成
1 parent 51f57bf commit a48a743

File tree

2 files changed

+54
-25
lines changed

2 files changed

+54
-25
lines changed

src/App.js

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,60 @@
1-
import logo from './logo.svg';
2-
import './App.css';
1+
import { useState } from 'react';
2+
3+
const App = () => {
4+
console.log('renders <App />');
5+
6+
const [item, setItem] = useState('');
7+
const [cartItems, setCartItems] = useState([]);
8+
9+
const handleChange = (event) => {
10+
setItem(event.target.value);
11+
};
12+
13+
const handleAdd = () => {
14+
if (cartItems.includes(item)) {
15+
return;
16+
}
17+
18+
setCartItems((items) => [...items, item]);
19+
setItem('');
20+
};
321

4-
function App() {
522
return (
6-
<div className="App">
7-
<header className="App-header">
8-
<img src={logo} className="App-logo" alt="logo" />
9-
<p>
10-
Edit <code>src/App.js</code> and save to reload.
11-
</p>
12-
<a
13-
className="App-link"
14-
href="https://reactjs.org"
15-
target="_blank"
16-
rel="noopener noreferrer"
17-
>
18-
Learn React
19-
</a>
20-
</header>
23+
<div style={{ margin: '10px' }}>
24+
<h1>商品を購入</h1>
25+
<Input value={item} onChange={handleChange} />
26+
<AddButton disabled={item.trim() === ''} onClick={handleAdd} />
27+
<ul>
28+
{cartItems.map((item) => (
29+
<li>{item}</li>
30+
))}
31+
</ul>
2132
</div>
2233
);
23-
}
34+
};
35+
36+
const Input = ({ value, onChange }) => {
37+
console.log('renders <Input />');
38+
39+
return (
40+
<p>
41+
<label>
42+
アイテム名: <input type="text" value={value} onChange={onChange} />
43+
</label>
44+
</p>
45+
);
46+
};
47+
48+
const AddButton = ({ disabled, onClick }) => {
49+
console.log('renders <AddButton />');
50+
51+
return (
52+
<p>
53+
<button disabled={disabled} onClick={onClick}>
54+
カートに追加する
55+
</button>
56+
</p>
57+
);
58+
};
2459

2560
export default App;

src/index.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@ import React from 'react';
22
import ReactDOM from 'react-dom';
33
import './index.css';
44
import App from './App';
5-
import reportWebVitals from './reportWebVitals';
65

76
ReactDOM.render(
87
<React.StrictMode>
98
<App />
109
</React.StrictMode>,
1110
document.getElementById('root')
1211
);
13-
14-
// If you want to start measuring performance in your app, pass a function
15-
// to log results (for example: reportWebVitals(console.log))
16-
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
17-
reportWebVitals();

0 commit comments

Comments
 (0)