본문 바로가기

Front End/React

[React Exam 10] HTML로 시작해보는 React 보너스(천자문 보기)

다음은 완성된 전체 코드 입니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<!DOCTYPE html>
<html lang="ko">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>React 예제 011</title>
    <script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    <style>
        .item {
            padding:5px;
            width: 260px;
            border: 1px solid gray;
            margin: 5px;
            border-radius: 30px;
            float: left;
            text-align: center;
        }
    </style>
    <script type="text/babel">
        function Hanja(props) {
            return (
                <div className="item">
                    <div style={{fontSize: "80px"}}>
                        {props.hanja.h}
                    </div>
                    <hr />
                    <div>
                        {(props.hanja.index + "").padStart(4,0)}. {props.hanja.t}
                    </div>
                </div>
            );
        }
        window.onload = () =>{
            fetch("https://joongwoonc.github.io/data/chunja.json")
                .then((response) => {
                    console.log(response);
                    return response.json();
                }).then((json) => {
                    ReactDOM.createRoot(document.querySelector('#app')).render(
                        <div>
                            <h1>천자문(千字文)</h1>
                            <hr/>                        
                            {
                                json.map((hanja) => {
                                    return <Hanja key={hanja.index} hanja={hanja} />
                                })
                            }
                        </div>
                    );
                });
        }
 
    </script>
</head>
 
<body>
    <div id="app"></div>
</body>
 
</html>
cs

 

다음은 실행 결과 입니다.