🐱 #2. React.js - JSX

박쀀석·2022λ…„ 10μ›” 13일
1

React

λͺ©λ‘ 보기
2/13
post-thumbnail

μ˜€λŠ˜μ€ JSXμ—μ„œ 배울 것 이닀. 이전에 λ°°μ› λ˜ μ•„λž˜μ˜ 문법을 더 μ‰½κ²Œ μ“°λŠ” 방법이닀.

        const h3 = React.createElement("h3", {
            id : "title",
            onMouseEnter:() => console.log("mouseenter")
        }, 
            "Hello i'm a span"
            );

μš°μ„  JSX λ°©μ‹μœΌλ‘œ λ‹€μ‹œ μ½”λ”©ν•˜λ©΄ μ•„λž˜μ™€ κ°™λ‹€. ν™”μ‚΄ν‘œ ν•¨μˆ˜λ₯Ό μ¨μ„œ μž‘μ„±ν•œλ‹€.

 const Title = () => (
            <h3 id="title" onMouseEnter={() => console.log("mouseenter")}>
                Hello i'm a title
            </h3>
        );

ν•˜μ§€λ§Œ μœ„μ™€ 같이 μž‘μ„±ν•˜λ©΄ μ—λŸ¬κ°€ λœ¨λŠ” 것을 λ³Ό 수 μžˆλ‹€.

μ—λŸ¬κ°€ λœ¨λŠ” μ΄μœ λŠ” 컴퓨터가 JSX 문법을 읽지 λͺ»ν•˜λŠ” 것이닀.

μœ„μ™€ 같이 JSX 문법을 λ³€ν™˜ν•΄μ„œ 컴퓨터가 읽게 ν•΄μ€˜μ•Όν•œλ‹€. 이것을 λ³€ν™˜ν•˜λŠ” Babel JSνŒŒμΌμ„ μž„ν¬νŠΈ μ‹œν‚€κ³  script type을 μ§€μ •ν•˜λ©΄ λœλ‹€. μ–΄λ–»κ²Œ μ§€μ •ν•˜λŠ”μ§€ 보자


μ•„λž˜μ™€ 같이 babel.jsλ₯Ό μž„ν¬νŠΈ μ‹œν‚€κ³  script에 type="text/babel"을 지정해쀀닀.

그리고 기쑴에 뢈러올 λ•Œ μ•„λž˜μ™€ 같은 방법을 썼닀면 JSX 문법은

const container = React.createElement("div", null, [h3, btn])

        ReactDOM.render(container, root);

μ•„λž˜μ™€ κ°™λ‹€.

 const Container = () => <div>
                <Title />
                <Button /> 
            </div>
  ReactDOM.render(<Container />, root);

μ£Όμ˜μ μ€ μ»΄ν¬λ„ŒνŠΈμ˜ 첫 κΈ€μžλŠ” λ°˜λ“œμ‹œ λŒ€λ¬Έμžμ—¬μ•Ό ν•œλ‹€.

JSX 문법을 μ“°λ©΄ 쒀더 κ°„κ²°ν•˜κ²Œ μ½”λ”©ν•  수 μžˆλ‹€. λ¦¬μ•‘νŠΈ λ§Œμ„Έ

profile
μ•ˆλ…•ν•˜μ„Έμš” ν”„λ‘ νŠΈμ—”λ“œ κ°œλ°œμžμž…λ‹ˆλ‹€. 글을 이전 μ€‘μž…λ‹ˆλ‹€.

2개의 λŒ“κΈ€

comment-user-thumbnail
2022λ…„ 10μ›” 28일

10000μ„Έ!

1개의 λ‹΅κΈ€