22-07-04 | useState로 상태변경

MOON HEE·2022년 7월 4일
0

import { useState } from "react";

function App() {
    const [name, setName] = useState("태태룽");
    let color = "white";

    function newColor() {
        color = color === "white" ? "black" : "white";
        document.querySelector("#color").textContent = color;
    }

    return (
        <>
        <h1>Study State</h1>
        <h2 id="name">{name}</h2>
        <p id="color">{color}</p>
        <button
            onClick={() => {
            setName(name === "그린티" ? "태태룽" : "그린티");
            }}
        >
            Change1
        </button>
        <button onClick={newColor}>Change2</button>
        </>
    );
}

export default App;
profile
자고 일어나면 해결되는게 더 많은 듯 그럼 잘까

0개의 댓글