[노마드코더] React JS - State Function

김해김씨가오리·2023년 10월 23일
0

노마드코더

목록 보기
2/7

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(counter + 1);
            // 직접 값을 설정해주는 법
            setCounter((current) => current + 1)
            // react가 이 current가 현재값임을 정확히 알 수 있음
            // 함수를 전달해주는 법
        };
        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로 영화 웹 서비스 만들기

profile
그냥 기록용!!!

0개의 댓글