IT EDU/REACT

[React] React ๊ธฐ์ดˆ

yoonhoou 2022. 3. 25.
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

๋Œ“๊ธ€