24. ๋ฆฌ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๋™์  ์ถœ๋ ฅ

  • ํ”„๋กœ์ ํŠธ ๋งํฌopen in new window

  • ๋ฆฌ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๋™์  ์ถœ๋ ฅ

    • map() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    • key ์†์„ฑ์€ ๋ฆฌ์•กํŠธ๊ฐ€ ๊ฐ ์š”์†Œ๋ฅผ ์‹๋ณ„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๊ณ ์œ ํ•œ ์‹๋ณ„์ž์ด๋‹ค.
    src/App.tsx
    import { useState } from "react";
    import TabButton from "./TabButton";
    import { EXAMPLES } from "./data";
    import coreConcepts from './components/CoreConcepts';
    import {CORE_CONCEPTS} from './data';
    
    function App() {
      const [selectedTopic, setSelectedTopic] = useState("components");
    
      function handleSelect(selectedButton) {
        setSelectedTopic(selectedButton);
        console.log(selectedTopic);
      }
    
      return (
        <div>
          <section id="core-concepts">
          <h2>Core Concepts</h2>
          <ul>
            {CORE_CONCEPTS.map((concept) => (
              <CoreConcepts key={concept.title} {...concept} />
            ))}
          </ul>
          </section>
          <section id="examples">
          <h2>Examples</h2>
          <menu>
            <TabButton 
              isSelected={selectedTopic === 'components'} 
              onSelect={() => handleSelect('components')}
            >
              Components
            </TabButton>
            <TabButton 
              isSelected={selectedTopic === 'jsx'} 
              onSelect={() => handleSelect('jsx')}
            >
              JSX
            </TabButton>
            <TabButton 
              isSelected={selectedTopic === 'props'} 
              onSelect={() => handleSelect('props')}
            >
              Props
            </TabButton>
            <TabButton 
              isSelected={selectedTopic === 'state'} 
              onSelect={() => handleSelect('state')}
            >
              State
            </TabButton>
          </menu>
          {!selectedTopic ? <p>Please select a topic.</p> : (
            <div id="tab-content">
              <h3>{EXAMPLES[selectedTopic].title}</h3>
              <p>{EXAMPLES[selectedTopic].description}</p>
              <pre>
                <code>
                  {EXAMPLES[selectedTopic].code}
                </code>
              </pre>
            </div>
            )
          }
        </section>
        </div>
      );
    }