import { useState } from 'react';
const [state, setState] = useState(initialState);
const [num, setNum] = useState(1);
//App.js
import { useState } from 'react';
import Button from './Button';
import Dice from './Dice'
function random(n){
// 1~n까지의 랜덤한 값 리턴
return Math.ceil(Math.random()*n)
};
function App(){
const [num, setNum] = useState(1);
const handleRollClick = () => {
const nextNum = random(6);
setNum(nextNum);
};
const handleClearClick = () => {
setNum(1);
};
return (
<div>
<div>
<Button onClick={handleRollClick} >던지기</Button>
<Button onClick={handleClearClick}>처음부터</Button>
</div>
<Dice color="blue" num={num} />
</div>
);
}
export default App;
//Button.js
function Button ({children, onClick}){
return <button onClick={onClick} > {children}</button>;
}
export default Button;
//App.js
import { useState } from 'react';
import Button from './Button';
import Dice from './Dice'
function random(n){
// 1~n까지의 랜덤한 값 리턴
return Math.ceil(Math.random()*n);
};
function App(){
const [num, setNum] = useState(1);
const [sum, setSum] = useState(0);
const [gameHistory, setGameHistory] = useState([]);
const handleRollClick = () => {
const nextNum = random(6);
setNum(nextNum);
setSum(sum + nextNum);
gameHistory.push(nextNum);
setGameHistory(gameHistory);
};
const handleClearClick = () => {
setNum(1);
setSum(0);
setGameHistory([]);
};
return (
<div>
<div>
<Button onClick={handleRollClick} >던지기</Button>
<Button onClick={handleClearClick}>처음부터</Button>
</div>
<div>
<h2>나</h2>
<Dice color="blue" num={num} />
<h2>총점</h2>
<p>
{sum}
</p>
<h2>기록</h2>
<p>
{gameHistory.join(', ')}
</p>
</div>
</div>
);
}
export default App;
//App.js
...
const handleRollClick = () => {
const nextNum = random(6);
//setNum(nextNum);
//setSum(sum + nextNum);
gameHistory.push(nextNum);
setGameHistory(gameHistory);
};
...
import { useState } from 'react';
import Button from './Button';
import Dice from './Dice'
function random(n){
// 1~n까지의 랜덤한 값 리턴
return Math.ceil(Math.random()*n);
};
function App(){
const [num, setNum] = useState(1);
const [sum, setSum] = useState(0);
const [gameHistory, setGameHistory] = useState([]);
const handleRollClick = () => {
const nextNum = random(6);
setNum(nextNum);
setSum(sum + nextNum);
//gameHistory.push(nextNum);
setGameHistory([...gameHistory, nextNum]);
};
const handleClearClick = () => {
setNum(1);
setSum(0);
setGameHistory([]);
};
return (
<div>
<div>
<Button onClick={handleRollClick} >던지기</Button>
<Button onClick={handleClearClick}>처음부터</Button>
</div>
<div>
<h2>나</h2>
<Dice color="blue" num={num} />
<h2>총점</h2>
<p>
{sum}
</p>
<h2>기록</h2>
<p>
{gameHistory.join(', ')}
</p>
</div>
</div>
);
}
export default App;
import { useState } from 'react';
const [state, setState] = useState(initialState);
const [num, setNum] = useState(1);
//App.js
import { useState } from 'react';
import Button from './Button';
import Dice from './Dice'
function random(n){
// 1~n까지의 랜덤한 값 리턴
return Math.ceil(Math.random()*n)
};
function App(){
const [num, setNum] = useState(1);
const handleRollClick = () => {
const nextNum = random(6);
setNum(nextNum);
};
const handleClearClick = () => {
setNum(1);
};
return (
<div>
<div>
<Button onClick={handleRollClick} >던지기</Button>
<Button onClick={handleClearClick}>처음부터</Button>
</div>
<Dice color="blue" num={num} />
</div>
);
}
export default App;
//Button.js
function Button ({children, onClick}){
return <button onClick={onClick} > {children}</button>;
}
export default Button;
//App.js
import { useState } from 'react';
import Button from './Button';
import Dice from './Dice'
function random(n){
// 1~n까지의 랜덤한 값 리턴
return Math.ceil(Math.random()*n);
};
function App(){
const [num, setNum] = useState(1);
const [sum, setSum] = useState(0);
const [gameHistory, setGameHistory] = useState([]);
const handleRollClick = () => {
const nextNum = random(6);
setNum(nextNum);
setSum(sum + nextNum);
gameHistory.push(nextNum);
setGameHistory(gameHistory);
};
const handleClearClick = () => {
setNum(1);
setSum(0);
setGameHistory([]);
};
return (
<div>
<div>
<Button onClick={handleRollClick} >던지기</Button>
<Button onClick={handleClearClick}>처음부터</Button>
</div>
<div>
<h2>나</h2>
<Dice color="blue" num={num} />
<h2>총점</h2>
<p>
{sum}
</p>
<h2>기록</h2>
<p>
{gameHistory.join(', ')}
</p>
</div>
</div>
);
}
export default App;
//App.js
...
const handleRollClick = () => {
const nextNum = random(6);
//setNum(nextNum);
//setSum(sum + nextNum);
gameHistory.push(nextNum);
setGameHistory(gameHistory);
};
...
import { useState } from 'react';
import Button from './Button';
import Dice from './Dice'
function random(n){
// 1~n까지의 랜덤한 값 리턴
return Math.ceil(Math.random()*n);
};
function App(){
const [num, setNum] = useState(1);
const [sum, setSum] = useState(0);
const [gameHistory, setGameHistory] = useState([]);
const handleRollClick = () => {
const nextNum = random(6);
setNum(nextNum);
setSum(sum + nextNum);
//gameHistory.push(nextNum);
setGameHistory([...gameHistory, nextNum]);
};
const handleClearClick = () => {
setNum(1);
setSum(0);
setGameHistory([]);
};
return (
<div>
<div>
<Button onClick={handleRollClick} >던지기</Button>
<Button onClick={handleClearClick}>처음부터</Button>
</div>
<div>
<h2>나</h2>
<Dice color="blue" num={num} />
<h2>총점</h2>
<p>
{sum}
</p>
<h2>기록</h2>
<p>
{gameHistory.join(', ')}
</p>
</div>
</div>
);
}
export default App;
import { useState } from 'react';
const [state, setState] = useState(initialState);
const [num, setNum] = useState(1);
//App.js
import { useState } from 'react';
import Button from './Button';
import Dice from './Dice'
function random(n){
// 1~n까지의 랜덤한 값 리턴
return Math.ceil(Math.random()*n)
};
function App(){
const [num, setNum] = useState(1);
const handleRollClick = () => {
const nextNum = random(6);
setNum(nextNum);
};
const handleClearClick = () => {
setNum(1);
};
return (
<div>
<div>
<Button onClick={handleRollClick} >던지기</Button>
<Button onClick={handleClearClick}>처음부터</Button>
</div>
<Dice color="blue" num={num} />
</div>
);
}
export default App;
//Button.js
function Button ({children, onClick}){
return <button onClick={onClick} > {children}</button>;
}
export default Button;