728x90
๐ป Component
1. React Class Component(ํด๋์คํ ์ปดํฌ๋ํธ)
snippet : rcc
import React, { Component } from "react";
// classํ component
class App extends Component {
render() {
return <div>
</div>;
}
}
export default App;
2. React Stateless Function(ํจ์ํ ์ปดํฌ๋ํธ)
snippet : rsf
import React from 'react';
// ํจ์ํ component
function App(props) {
return (
<div>
</div>
);
}
export default App;
snippet : rsc(๋๋คํ)
import React from "react";
// ํจ์ํ ์ปดํฌ๋ํธ
const App = ()=> {
return (
<div>
</div>
);
}
export default App;
๐จ๐ป ์์
[1] .jsx์ html ์ ์ฉํ๊ธฐ
import React from 'react';
function App() {
// ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋ ์์ญ
console.log("Start");
return (
<div>
{/*HTML(JSX) ์ฝ๋๊ฐ ๋ค์ด๊ฐ๋ ๊ณณ*/}
<h1>Start React 200!</h1>
<p>HTML ์ ์ฉํ๊ธฐ</p>
</div>
);
}
export default App;
[2] .jsx์ html ์ ์ฉํ๊ธฐ
import React, { Component } from "react";
class App extends Component {
// ํ๋ ์ ์ธ์ด ๊ฐ๋ฅํ ๋ถ๋ถ
name = "ํธ๋์ด";
render() {
console.log("sample");
return (
<div>
<h1>ํธ๋์ด</h1>
</div>
);
}
}
export default App;
[3] .jsx์ css ์ ์ฉํ๊ธฐ
App.js
import React from "react";
import "./App.css";
function App() {
return (
<div>
<h1>Start React 200!</h1>
<p>HTML ์ ์ฉํ๊ธฐ</p>
</div>
);
}
export default App;
App.css
div {
background-color: rgb(162, 127, 243);
color: rgb(255, 255, 255);
padding: 40px;
font-family: ๊ณ ๋;
text-align: center;
}
h1 {
color: white;
background-color: #2efe2e;
padding: 10px;
font-family: ๊ถ์;
}
[4] Component ์ฌ์ฉํ๊ธฐ
Component๋ , ํน์ ์ฝ๋ ๋ญ์น๋ฅผ ๋ค๋ฅธ ๋ถ๋ถ์ ์ด์ํ๊ฑฐ๋ ์ฌ์ฌ์ฉํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ์ฝ๋ ๋ธ๋ก ๋จ์๋ฅผ ๋งํ๋ค.
Bpp.js
import React, { Component } from 'react';
class Bpp extends Component {
render() {
return (
<h2>[THIS IS IMPORTED COMPONENT]</h2>
);
}
}
export default Bpp;
App.js
import React from "react";
import "./App.css";
import ImportComponent from './Bpp'
// Bpp.js ํ์ผ์์ export default ๋ก ๋์ด์๋ ์ปดํฌ๋ํธ๋ฅผ ๊ฐ์ง๊ณ ์ค์์ค
// ๊ฐ์ ธ์จ ์ปดํฌ๋ํธ ์ด๋ฆ์ ์ฌ๊ธฐ์๋ Bpp๋ผ๋ ์ด๋ฆ์ผ๋ก ์ฌ์ฉํ ๊ฒ์ด๋ค.
function App() {
return (
<div>
<h1>Start React 200!</h1>
<p>HTML ์ ์ฉํ๊ธฐ</p>
<ImportComponent></ImportComponent>
</div>
);
}
export default App;
'IT EDU > REACT' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React] React Lifecycle - ์๋ช ์ฃผ๊ธฐ ํจ์ (0) | 2022.03.28 |
---|---|
[React] React ์ค์น ๋ฐ ๊ธฐ์ด ์์ (0) | 2022.03.25 |
๋๊ธ