3.8 Recap
2022. 9. 14. 15:01ㆍReact/ReactJS로 영화 웹 서비스 만들기
먼저 flipped와 setFlipped를 invert, setInvert로 변경하고,
button을 아래와 같이 변경하자.
<button onClick={onInvert}>{inverted ? "Turn back" : "Invert"}</button>
<!DOCTYPE html>
<html lang="en">
<body>
<div id="root"></div>
</body>
<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script type="text/babel">
const root = document.getElementById("root");
function App() {
const [amount, setAmount] = React.useState(0);
const [inverted, setInverted] = React.useState(false);
const onChange = (event) => {
setAmount(event.target.value);
}
const reset = () => setAmount(0);
const onInvert = () => {
reset();
setInverted((current) => !current);
};
return (
<div>
<h1>Super Converter</h1>
<div>
<label htmlFor="minutes">Minutes</label>
<input
value={inverted ? amount * 60 : amount}
id="minutes"
placeholder="Minutes"
type="number"
onChange={onChange}
disabled={inverted}
/>
</div>
<div>
<label htmlFor="hours">Hours</label>
<input
value={inverted ? amount : Math.round(amount / 60)}
id="hours"
placeholder="Hours"
type="number"
onChange={onChange}
disabled={!inverted}
/>
</div>
<button onClick={reset}>Reset</button>
<button onClick={onInvert}>{inverted ? "Turn back" : "Invert"}</button>
</div>
)
}
ReactDOM.render(<App />, root);
</script>
</html>
그러면 각 변할때마다 버튼이 변하게 된다.
'React > ReactJS로 영화 웹 서비스 만들기' 카테고리의 다른 글
4.0 Props (0) | 2022.09.14 |
---|---|
3.9 Final Practice and Recap (0) | 2022.09.14 |
3.7 State Practice part Two (0) | 2022.09.14 |
3.6 State Practice part One (0) | 2022.09.13 |
3.5 Inputs and State (0) | 2022.09.13 |