useState를 보다 안정적으로 사용하기..
<!DOCTYPE html>
<html lang="ko">
<body>
<div id="root"></div>
</body>
<script src="https://unpkg.com/react@17.0.2/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@17.0.2/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 [counter, setCounter] = React.useState(0);
const onClick = () => {
setCounter((current) => current + 1)
};
return (
<div>
<h3>Total clicks : {counter}</h3>
<button onClick = {onClick}>Click me</button>
</div>
);
}
ReactDOM.render(<App />, root)
</script>
</html>
making converter
<!DOCTYPE html>
<html lang="ko">
<body>
<div id="root"></div>
</body>
<script src="https://unpkg.com/react@17.0.2/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@17.0.2/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();
const [flipped, setFlipped] = React.useState(false);
const onChange = (event) => {
setAmount(event.target.value);
}
function reset() {
setAmount(0)
}
function onFlip() {
reset();
setFlipped((current) => !current);
}
return (
<div>
<h1 className="hi">Super Converter</h1>
<div>
<label htmlFor="minutes">Minutes</label>
<input
value={flipped ? amount * 60 : amount}
classId="minutes"
placeholder="Minutes"
type="number"
onChange={onChange}
disabled={flipped}
/>
<h4>You want to convert {amount}</h4>
<label htmlFor="hours">Hours</label>
<input
classId="hours"
placeholder="hours"
type="number"
onChange={onChange}
value={flipped ? amount : Math.round(amount / 60)}
disabled={!flipped}
/>
</div>
<button onClick={reset}>Reset</button>
<button onClick={onFlip}>flip</button>
</div>
);
}
ReactDOM.render(<App />, root)
</script>
</html>
출처
: ReactJS로 영화 웹 서비스 만들기