21. 데이터 기반 State(상태) 가져오기 및 출력

  • 프로젝트 링크open in new window

  • 데이터 기반 State(상태) 가져오기 및 출력

    src/App.tsx
    import { useState } from "react";
    import TabButton from "./TabButton";
    import { EXAMPLES } from "./data";
    
    function App() {
      const [selectedTopic, setSelectedTopic] = useState("components");
    
      function handleSelect(selectedButton) {
        setSelectedTopic(selectedButton);
        console.log(selectedTopic);
      }
    
      return (
        <section id="examples">
          <h2>Examples</h2>
          <menu>
            <TabButton onSelect={() => handleSelect('components')}>Components</TabButton>
            <TabButton onSelect={() => handleSelect('jsx')}>JSX</TabButton>
            <TabButton onSelect={() => handleSelect('props')}>Props</TabButton>
            <TabButton onSelect={() => handleSelect('state')}>State</TabButton>
          </menu>
          <div id="tab-content">
            <h3>{EXAMPLES[selectedTopic].title}</h3>
            <p>{EXAMPLES[selectedTopic].description}</p>
            <pre>
              <code>
                {EXAMPLES[selectedTopic].code}
              </code>
            </pre>
          </div>
        </section>
      );
    }