본문 바로가기

Frontend/React

Unit Converter

<!DOCTYPE html>
<html>
    <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">
    function MinutesToHours() {
        const [amount, setAmount] = React.useState(0);
        const [inverted, setInverted] = React.useState(false);
        const onChange = () => {
            setAmount(event.target.value);
        };
        const reset = () => setAmount(0);
        const onInvert = () => {
            reset();
            setInverted((current)=>!current);  // true <-> false
        }
        return  (
        <div>
         <div>  
        <label htmlfor="minutes">Minutes</label>
        <input 
        value={inverted ? amount*60 : amount} 
        id="minutes" 
        placeholder= "Minutes" 
        type="number" 
        onChange={onChange}
        disabled = {inverted} // === {inverted = true}
        />
        </div>
        <div>
        <label htmlfor="hours">Hours</label>
        <input 
        value={inverted ? amount : Math.round(amount/60)} 
        id="hours" 
        placeholder= "Hours" 
        type="number" 
        disabled = {!inverted} // === {inverted = false}
        onChange={onChange}
        />
        </div>
        <button onClick={reset}>Reset</button>
        <button onClick={onInvert}>{inverted ? "Turn back" : "Invert"}</button>
        </div>
        );
    }
    function KmToMiles(){
        const [amount, setAmount] = React.useState(0);
        const [inverted, setInverted] = React.useState(false);
        const onChange = () => {
            setAmount(event.target.value);
        };
        const reset = () => setAmount(0);
        const onInvert = () => {
            reset();
            setInverted((current)=>!current);  // true <-> false
        }
        return  (
        <div>
         <div>  
        <label htmlfor="kilometer">Kilometer</label>
        <input 
        value={inverted ? amount*1.60934 : amount} 
        id="kilometer" 
        placeholder= "Kilometer" 
        type="number" 
        onChange={onChange}
        disabled = {inverted} // === {inverted = true}
        />
        </div>
        <div>
        <label htmlfor="mile">Mile</label>
        <input 
        value={inverted ? amount : amount/1.60934} 
        id="mile" 
        placeholder= "Mile" 
        type="number" 
        disabled = {!inverted} // === {inverted = false}
        onChange={onChange}
        />
        </div>
        <button onClick={reset}>Reset</button>
        <button onClick={onInvert}>{inverted ? "Turn back" : "Invert"}</button>
        </div>
        );

    }
    function App() {
        const [index, setIndex] = React.useState("xx")
        const onSelect = (event) => {
            setIndex(event.target.value);  //Minutes & Hours:0,KM & Miles:1
        }
        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>
            <hr />
            {index === "0" ? <MinutesToHours /> : null} 
            {index === "1" ? <KmToMiles /> : null}
        </div>
        );
    }
    const root = document.getElementById("root");
     ReactDOM.render(<App />, root);
    </script>
</html>​

 

'Frontend > React' 카테고리의 다른 글

To Do List (Enrollent)  (0) 2022.01.22
useEffect  (0) 2022.01.21
Props & memo  (0) 2022.01.11
JSX란?  (0) 2022.01.04
React vs Vanilla JS  (0) 2022.01.03