JSX
2022, Mar 02
JSX
- jsx는 React의 생김새를 정의 할 때 사용하는 문법이며 HTML같이 생겼지만 실제로는 JavaScript이다.
- React는 JSX를 사용할 필요가 없지만 JavaScript코드 내에서 UI로 작업할 때 시각적 보조 수단으로 도움을 주며 오류 및 경고 메시지를 표시할 수 있다.
- react_without_jsx.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<title>Document</title>
</head>
<body>
<div id="root"></div>
<ul>
<h2>Things cats like</h2>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
<script>
// 코드의 가독성이 낮음
const Component = props => {
return React.createElement('div', null,
React.createElement('h2', null, 'Things cats like'),
React.createElement('ul', null,
React.createElement('li', null, 'Coffee'),
React.createElement('li', null, 'Tea'),
React.createElement('li', null, 'Milk')))
};
ReactDOM.render(React.createElement(Component, null, null), document.querySelector('#root'));
</script>
</body>
</html>
- react_with_jsx.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<title>Document</title>
</head>
<body>
<!-- babel 코드를 변환시켜주는 사이트?-->
<div id="root"></div>
<script type="text/babel">
// component명 : MyComponent
const MyComponent = () => {
return (
<ul>
<h2>Things cat like</h2>
<li>coffee</li>
<li>tea</li>
<li>milk</li>
</ul>
);
};
ReactDOM.render(<MyComponent />, document.querySelector('#root'));
</script>
</body>
</html>