React Basic - hochan222/Everything-in-JavaScript GitHub Wiki

No. Question
1 What is ReactJS?
2 What are the major features of ReactJS?
3 What is JSX?
4 What is the difference between Element and Component?
5 How to create components in ReactJS?
6 When to use a Class Component over a Functional Component?
7 What are Pure Components?
8 What is state in ReactJS?
9 What is props in ReactJS?
10 What is the difference between state and props?

1. What is ReactJS?

(React๋ž€ ๋ฌด์—‡์ธ๊ฐ€์š”?)

React๋Š” SPA (Single Page Application) ์ฆ‰, ๋‹จ์ผ ํŽ˜์ด์ง€ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌ์„ฑ๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์˜คํ”ˆ ์†Œ์Šค ํ”„๋ก ํŠธ์—”๋“œ JS ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž…๋‹ˆ๋‹ค.
์›น ๋ฐ ๋ชจ๋ฐ”์ผ ์•ฑ์˜ Layer ๋ฅผ ๋‹ค๋ฃจ๋Š”๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋ฆฌ์•กํŠธ๋Š” ํŽ˜์ด์Šค๋ถ์˜ ์†Œํ”„ํŠธ์›จ์–ด ์—”์ง€๋‹ˆ์–ด Jordan Walke ์— ์˜ํ•ด ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค.
๋ฆฌ์•กํŠธ๋Š” 2011 ๋…„ ํŽ˜์ด์Šค๋ถ ๋‰ด์Šคํ”ผ๋“œ์— ๋ฐœํ‘œ๋˜์—ˆ๊ณ , 2012๋…„ ์ธ์Šคํƒ€๊ทธ๋žจ์— ์ฒ˜์Œ ๊ตฌ์ถ•๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

2. What are the major features of ReactJS?

(React์˜ ํŠน์ง•์€ ๋ฌด์—‡์ด ์žˆ์„๊นŒ์š”?)

React ์˜ ์ฃผ์š” ํŠน์ง•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • RealDOM ์„ ์กฐ์ž‘ํ•˜๋Š”๋ฐ ๋งŽ์€ ๋น„์šฉ์ด ๋“ค์–ด๊ฐ„๋‹ค๋Š” ์ ์„ ๊ณ ๋ คํ•˜์—ฌ ๋ฆฌ์•กํŠธ๋Š” RealDOM ๋Œ€์‹  VirtualDOM์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    • Virtual DOM์ด๋ผ๋Š” ๊ฑธ ๋„์ž…ํ•˜์—ฌ DOM์„ ์ง์ ‘ ๊ณ„์‚ฐํ•˜์ง€ ์•Š๊ณ  ๋‚ด๋ถ€์ ์œผ๋กœ ๋น ๋ฅด๊ฒŒ diff๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋งŒ๋“ค์—ˆ๋‹ค. ์ฒ˜์Œ์—๋Š” O(n3) ์ด์˜€์œผ๋‚˜ ๊ฒฐ๊ตญ O(n)์œผ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค.
  • ์„œ๋ฒ„ ์‚ฌ์ด๋“œ ๋ Œ๋”๋ง์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.
    • React๋Š” ์ฒ˜์Œ๋ถ€ํ„ฐ ์„œ๋ฒ„ ์‚ฌ์ด๋“œ ๋ Œ๋”๋ง์„ ๊ณ ๋ คํ•˜์—ฌ ์„ค๊ณ„ํ–ˆ๋‹ค.
  • ๋‹จ๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ํ๋ฆ„ ๋˜๋Š” ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ์„ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.
    • props, useState
  • UI ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

3. What is JSX?

(JSX๋ž€ ๋ฌด์—‡์ธ๊ฐ€์š”?)

JSX ๋Š” JS XML (ECMAScript๋กœ XML ์œ ์‚ฌ ๊ตฌ๋ฌธ ํ™•์žฅ) ์˜ ๊ตฌ๋ฌธ ํ‘œ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค. JSX ๋Š” JS XML์˜ ์•ฝ์ž์ž…๋‹ˆ๋‹ค.
HTML๊ณผ ๊ฐ™์€ ๋ฌธ๋ฒ•๊ณผ ํ•จ๊ป˜ JS๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ์•„๋ž˜์˜ h1 ํƒœ๊ทธ์•ˆ์— text ๋Š” render ํ•จ์ˆ˜์— ์˜ํ•ด JS ํ•จ์ˆ˜๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

render() {
  return (
    <div>
      <h1> Welcome to React world!!</h1>
    </div>
  );
}

4. What is the difference between Element and Component?

(element์™€ component์˜ ์ฐจ์ด์ ์€ ๋ฌด์—‡์ธ๊ฐ€์š”?)

element๋Š” DOM ๋…ธ๋“œ ๋˜๋Š” ๋‹ค๋ฅธ component๋“ค๊ณผ ๊ด€๋ จํ•˜์—ฌ ํ™”๋ฉด์— ํ‘œ์‹œ ํ•  ๋‚ด์šฉ์„ ํ‘œํ˜„ํ•˜๋Š” ์ผ๋ฐ˜ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. elements๋Š” ๋‹ค๋ฅธ elements ๋“ค์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. React element๋ฅผ ๋งŒ๋“œ๋Š” ๋น„์šฉ์€ ์ €๋ ดํ•ฉ๋‹ˆ๋‹ค. element ๋Š” ์ƒ์„ฑ๋˜๋ฉด ๋ณ€ํ˜•๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ฆฌ์•กํŠธ element ์˜ ํ‘œํ˜„์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

const element = React.createElement("div", { id: "login-btn" }, "Login");

์œ„์—์„œ ๋งŒ๋“ค์–ด์ง„ element ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฐ์ฒด๋กœ ๋ฆฌํ„ด๋ฉ๋‹ˆ๋‹ค.

{
  type: 'div',
  props: {
    children: 'Login',
    id: 'login-btn'
  }
}

๋งˆ์ง€๋ง‰์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ReactDOM.render ๋ฅผ ์ด์šฉํ•˜์—ฌ DOM ์œผ๋กœ ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค.

<div id="login-btn">Login</div>

๋ฐ˜๋ฉด component ๋Š” ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์œผ๋กœ ์„ ์–ธ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. render ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š” class ์ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ component ์ผ ๊ฒฝ์šฐ function์œผ๋กœ ์ •์˜ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ๋œ component ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ element ํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์— JSX ๋Š” createElement๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค.

function Button({ onLogin }) {
  return React.createElement(
    "div",
    { id: "login-btn", onClick: onLogin },
    "Login"
  );
}

5. How to create components in ReactJS?

(React์—์„œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์–ด๋–ป๊ฒŒ ์ƒ์„ฑํ•˜๋‚˜์š”?)

ReactJS ๋Š” Components ์ƒ์„ฑํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

Functional components

ReactJS ์—์„œ component๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. props ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ณ  React elements ๋ฅผ ๋ฆฌํ„ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์„ pure ํ•œ JS์˜ function ์ด๊ธฐ ๋•Œ๋ฌธ์— functionalํ•œ ์ƒ์„ฑ๋ฒ•์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค.

function Greeting(props) {
  return <h1> Hello, {props.message}</h1>;
}

Class components

ES6 Class๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ component ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ๋ณธ functional ์„ ์–ธ๋ฒ•์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

class Greeting extends React.Component {
  render() {
    return <h1>Hello, {this.props.message}</h1>;
  }
}

6. When to use a Class Component over a Functional Component?

(์–ธ์ œ ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์–ธ์ œ Functional ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ• ๊นŒ์š”?)

Component ์—์„œ state ๋˜๋Š” life cycle methods ๋ฅผ ํ•„์š”๋กœ ํ•œ๋‹ค๋ฉด Class component๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด Functional component๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

7. What are Pure Components?

(์ˆœ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋ž€ ๋ฌด์—‡์ธ๊ฐ€์š”?)

PureComponent๋Š” ๋™์ผํ•œ ์ƒํƒœ์—์„œ๋Š” ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. shouldComponentUpdate ๋ฉ”์„œ๋“œ๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์„ ์ œ์™ธํ•˜๊ณ ๋Š” component์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
props ๋˜๋Š” state ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ PureComponent ๋Š” state ์™€ props ์— ๋Œ€ํ•ด Shallow Compare์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
๋ฐ˜๋ฉด component๋Š” ํ˜„์žฌ props์™€ ๋ณ€ํ˜•๋  state๋ฅผ ๋น„๊ตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— component๋Š” shouldComponentUpdate๊ฐ€ ํ˜ธ์ถœ ๋  ๋•Œ ๋งˆ๋‹ค ๋‹ค์‹œ render ๋ฉ๋‹ˆ๋‹ค. (shouldComponentUpdate์˜ ๊ธฐ๋ณธ๊ฐ’์€ true ์ด๊ธฐ ๋•Œ๋ฌธ์—)

8. What is state in ReactJS?

(state๋ž€ ๋ฌด์—‡์ธ๊ฐ€์š”?)

Component State ๋Š” component์˜ life cycle ๋™์•ˆ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋Š” ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” state๋ฅผ ๊ฐ€๋Šฅํ•œ ๋‹จ์ˆœํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ  state ์˜ ๊ตฌ์„ฑ ์š”์†Œ์˜ ์ˆ˜๋ฅผ ์ตœ์†Œํ™”ํ•˜๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

message state๋ฅผ ๊ฐ€์ง„ user component ๋ฅผ ์ƒ์„ฑํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

class User extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      message: "Welcome to React world"
    };
  }
  render() {
    return (
      <div>
        <h1>{this.state.message}</h1>
      </div>
    );
  }
}

9. What is props in ReactJS?

(props๋ž€ ๋ฌด์—‡์ธ๊ฐ€์š”?)

Props ๋Š” HTML ํƒœ๊ทธ ์†์„ฑ๊ณผ ์œ ์‚ฌํ•œ ๊ทœ์น™์„ ์‚ฌ์šฉํ•˜์—ฌ React component ์— ์ „๋‹ฌ๋˜๋Š” ๊ฐ’์„ ํฌํ•จํ•˜๋Š” ๋‹จ์ผ ๊ฐ’ ๋˜๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ๋ถ€๋ชจ component ์—์„œ ์ž์‹ component ๋กœ ์ „๋‹ฌ๋˜๋Š” ๋ฐ์ดํ„ฐ ์ž…๋‹ˆ๋‹ค.

React์—์„œ props ์˜ ๋ชฉ์ ์€ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ component์— ์ œ๊ณตํ•˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

  1. ์‚ฌ์šฉ์ž ์ •์˜ ๋ฐ์ดํ„ฐ๋ฅผ React component ๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. State ๋ณ€๊ฒฝ์„ Trigger ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  3. Component ์˜ render ๋ฉ”์„œ๋“œ ์•ˆ์—์„œ this.props.reactProp ๋กœ ์ ‘๊ทผํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, reactProp๋ฅผ ๋ฐ›๋Š” element ๋ฅผ ๋งŒ๋“ค๊ณ 

<Element reactProp="1" />

์ด "reactProp" (๋˜๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ฐพ์€ ์ •์˜ํ•œ) props๋Š” React๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ๋œ component์—์„œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๊ณ , React์˜ native props ์˜์—ญ์— ํฌํ•จ๋œ ์†์„ฑ์ด ๋ฉ๋‹ˆ๋‹ค.

props.reactProp;

10. What is the difference between state and props?

(state์™€ props์˜ ์ฐจ์ด์ ์€ ๋ฌด์—‡์ธ๊ฐ€์š”?)

props์™€ state๋Š” ๋ชจ๋‘ JavaScript ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ๋‘๊ฐ€์ง€ ๋‹ค ๋ Œ๋”๋ง ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ, component ์™€ ๊ด€๋ จ๋œ ๊ธฐ๋Šฅ๋ฉด์—์„œ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
props๋Š” ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๊ฐ™์ด component ์š”์†Œ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. state ๋Š” component ์•ˆ์—์„œ ๊ด€๋ฆฌ๋˜๊ณ  ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜ ์„ ์–ธ๊ณผ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ