728x90
<!DOCTYPE html>
<html lang="en">
<body>
<script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script type="text/babel">
function MinutesToHours() {
const [amount, setAmount] = React.useState(0);
const [inverted, setInverted] = React.useState(false);
const onChange = (event) => {
setAmount(event.target.value);
//target = 방금 바뀐 Input
};
const reset = () => setAmount(0);
const onInvert = () => {
reset();
setInverted((current) => !current);
};
return (
<div>
<div>
<label htmlFor="minutes">Minutes</label>
<input
value={inverted ? amount*60 : amount} //event가 발생했을때 값을 업데이트해주고, UI에 표시해주는것
id="minutes"
placeholder="Minutes"
type="number"
onChange={onChange} //event를 실행해주는 것 하나
disabled={inverted}
/>
</div>
<div>
<label htmlFor="hours">Hours</label>
<input
value={inverted ? amount : amount/60}
id="hours"
placeholder="Hours"
type="number"
onChange={onChange}
disabled={!inverted}
/>
</div>
<button onClick={reset}>Reset</button>
<button onClick={onInvert}>{inverted ? "inverted_true" : "inverted_false"}</button>
</div>
);
}
function KmToMiles() {
const [amount, setAmount] = React.useState(0);
const [inverted, setInverted] = React.useState(false);
function onChange(event) {
setAmount(event.target.value);
}
const reset = () => setAmount(0);
const onInvert= () => {
reset();
setInverted((current) => !current);
}
return (
<div>
<div>
<label htmlFor="km">km</label>
<input
value={inverted ? amount*1.61 : amount}
id="km"
placeholder="Km"
type="number"
onChange={onChange}
disabled={inverted}
/>
</div>
<label htmlFor="miles">miles</label>
<input
value={inverted ? amount : amount*0.62}
id="miles"
placeholder="Miles"
type="number"
onChange={onChange}
disabled={!inverted}
/>
<div>
</div>
<button onClick={reset}>reset</button>
<button onClick={onInvert}>{inverted ? "inverted_true" : "inverted_false"}</button>
</div>
);
}
function App() {
const [index, setIndex] = React.useState("xx");
const onSelect = (event) => {
setIndex(event.target.value);
}
return (
<div>
<h1>Super Converter</h1>
<select value={index} onChange={onSelect}>
<option value="xx">Select your units</option>
<option value="0">Minutes & Hours </option>
<option value="1">Km & Miles </option>
</select>
{index === "xx" ? "Please Select your units" : null}
{index === "0" ? <MinutesToHours /> : null}
{index === "1" ? <KmToMiles /> : null}
</div>
)
};
const root = document.getElementById("root");
ReactDOM.render(<App />, root)
</script>
<div id="root">
</div>
</body>
</html>반응형
'React' 카테고리의 다른 글
| [React] create react 설치 후 (0) | 2022.11.09 |
|---|---|
| [React] props (0) | 2022.11.09 |
| [React] State2 (0) | 2022.11.09 |
| [React] State1 (1) | 2022.11.09 |
| [React] Event, JSX (1) | 2022.11.08 |
댓글