React

๊น€๋„๋„›ยท2021๋…„ 10์›” 21์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
2/5
post-thumbnail

๐Ÿค”๋ฆฌ์•กํŠธ ์‚ฌ์šฉ ์ด์œ 

๋จผ์ € ํŽ˜์ด์Šค๋ถ์˜ ์ƒ๋‹จ ๋ถ€๋ถ„์„ ๋ณด์ž
์กฐ๊ทธ๋งŒ ํ•œ์ค„ ๋ถ€๋ถ„์ด์ง€๋งŒ , ์‹ค์ œ ์ฝ”๋“œ๋ฅผ ๋ดค์„๋•Œ
์‹ค์ œ ์ฝ”๋“œ ๋ถ€๋ถ„์€ ์ƒ๋‹น๋Ÿ‰์„ ์ฐจ์ง€ํ•œ๋‹ค.
๊ทธ๋ ‡๋‹ค๋ฉด ์ด ๋ถ€๋ถ„์˜ ์ˆ˜์ •์ด ์ด๋ฃจ์–ด์ง„๋‹ค๋ฉด ํŽ˜์ด์ง€๋งˆ๋‹ค ์ € ๋ถ€๋ถ„์„ ๋ฐ”๊พธ์–ด ์ฃผ์–ด์•ผ ํ•˜๋‚˜?
๋˜ํ•œ ํŽ˜์ด์ง€ ์†Œ์Šค๋ฅผ ๋ณด๊ณ  ์–ด๋Š ๋ถ€๋ถ„์ด ์ € ์˜์—ญ์ธ์ง€ ์ฐพ๊ธฐ๊ฐ€ ํž˜๋“ค๋‹ค.

๋ฆฌ์•กํŠธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, ๊ฐ์ž์˜ ์˜์—ญ์„ ์‚ฌ์šฉ์ž ์ •์˜ ํƒœ๊ทธ(Component)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ
๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ณ  , ์žฌ์‚ฌ์šฉ์„ฑ์ด ๋†’์•„์ง€๊ณ , ์œ ์ง€ ๋ณด์ˆ˜์˜ ์žฅ์ ์ด ์ƒ๊ธด๋‹ค.
Component๋ฅผ ์•Œ์•„๋ณด๊ธฐ ์ „์— JSX๋ถ€ํ„ฐ ์•Œ์•„๋ณด์ž.

jsp์—์„œ๋„ ํŽ˜์ด์ง€ ๋ถ„ํ• ํ•ด์„œ ํ•˜์ง€์•Š๋‚˜? ๋ญ๊ฐ€ ๋” ์ข‹์•„์ง„๊ฑฐ์ง€ ๋ผ๋Š” ์˜๋ฌธ์ด ์ƒ๊น€

๋ฆฌ์•กํŠธ๋Š” ๊ฐ€์ƒ ๋”์„ ์‚ฌ์šฉํ•˜์—ฌ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์„ฑ๋Šฅ์„ ๊ทน๋Œ€ํ™”
๋ฆฌํ”Œ๋กœ์šฐ(Reflow):DOM ์กฐ์ž‘์œผ๋กœ ๋ ˆ์ด์•„์›ƒ ๊ณผ์ •์ด ๋‹ค์‹œ ์ง„ํ–‰๋˜๋Š” ๊ฒƒ
๋ฆฌํŽ˜์ธํŠธ(Repaint):ํŽ˜์ธํŒ… ๊ณผ์ •์ด ๋‹ค์‹œ ์ง„ํ–‰๋˜๋Š” ๊ฒƒ
ex)
์‚ฌ์šฉ์ž๊ฐ€ Todo ๋ฆฌ์ŠคํŠธ ์•ฑ์„ ์‚ฌ์šฉํ•  ๋•Œ,
์•„๋ฌด๊ฒƒ๋„ ์—†๋˜ ๋ฆฌ์ŠคํŠธ์— ํ• ์ผ์„ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•˜๋Š” ๊ณผ์ •์„ ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. ํ• ์ผ์„ ์ž…๋ ฅํ•˜๊ณ  ์ถ”๊ฐ€ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด, ์•„๋ฌด๊ฒƒ๋„ ์—†๋˜ ๋ฆฌ์ŠคํŠธ์— ํ• ์ผ์„ ํ•˜๋‚˜ ํ‘œ์‹œํ•˜๊ณ , ์ „์ฒด ํ• ์ผ ๊ฐœ์ˆ˜๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์ด ๋•Œ, ๊ฐ€์ƒ ๋”์ด ์—†๋Š” ๊ฒฝ์šฐ, ํ• ์ผ์„ ํ•˜๋‚˜ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ๋ฆฌํ”Œ๋กœ์šฐ/๋ฆฌํŽ˜์ธํŠธ๊ฐ€ ํ•œ ๋ฒˆ ๋ฐœ์ƒํ•˜๊ณ , ์ „์ฒด ํ• ์ผ ๊ฐœ์ˆ˜๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ๋ฆฌํ”Œ๋กœ์šฐ/๋ฆฌํŽ˜์ธํŠธ๊ฐ€ ๋‹ค์‹œ ํ•œ ๋ฒˆ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ, ๋ฆฌ์•กํŠธ์—์„œ๋Š” ์ด ๋ชจ๋“  ๊ณผ์ •์„ ๊ฐ€์ƒ ๋”์—์„œ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์‚ฌ์šฉ์ž๊ฐ€ ํ• ์ผ์„ ์ž…๋ ฅํ•˜๊ณ  ์ถ”๊ฐ€ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด, ํ• ์ผ ํ‘œ์‹œ, ์ „์ฒด ํ• ์ผ ๊ฐœ์ˆ˜๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ€์ƒ ๋”์—์„œ ๊ณ„์‚ฐ์„ ํ•œ ํ›„, ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ๋ธŒ๋ผ์šฐ์ €์— ์ „๋‹ฌํ•˜์—ฌ ๋ฆฌํ”Œ๋กœ์šฐ/๋ฆฌํŽ˜์ธํŠธ๋ฅผ ํ•œ ๋ฒˆ๋งŒ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

์ถœ์ฒ˜ : https://dev-yakuza.posstree.com/ko/react/create-react-app/react/


๐Ÿ˜Š JSX ๋Š” ๋ญ์•ผ?(Java Script XML)

๊ฒฐ๋ก 

1.ํŽ˜์ด์Šค๋ถ์—์„œ ๋งŒ๋“  ์ปดํ“จํ„ฐ ์–ธ์–ด์ฏค์ด๋ผ ์ƒ๊ฐ.
2.jsx๋ฅผ create-react-app์ด JavaScript๋กœ Converting.

JSX์˜ ex)

HTML๋„ ์•„๋‹ˆ๊ณ  , JavaScript๋„ ์•„๋‹˜,,

JS์˜ ex)

JS์—์„œํ™•์žฅ๋œ ๋ฌธ๋ฒ•์ด๋ผ๊ณ  ์ ํ˜€์žˆ์ง€๋งŒ, ๊ทœ์น™์€ ์‚ด์ง ๋‹ค๋ฅธ ๋Š๋‚Œ์ด๋‹ค.

๊ณต์‹ ๋ฌธ์„œ์˜ JSX์˜ ์„ค๋ช…

React์—์„œ๋Š” ๋ณธ์งˆ์ ์œผ๋กœ ๋ Œ๋”๋ง ๋กœ์ง์ด UI ๋กœ์ง(์ด๋ฒคํŠธ๊ฐ€ ์ฒ˜๋ฆฌ๋˜๋Š” ๋ฐฉ์‹, ์‹œ๊ฐ„์— ๋”ฐ๋ผ state๊ฐ€ ๋ณ€ํ•˜๋Š” ๋ฐฉ์‹, ํ™”๋ฉด์— ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๊ฐ€ ์ค€๋น„๋˜๋Š” ๋ฐฉ์‹ ๋“ฑ)๊ณผ ์—ฐ๊ฒฐ๋œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ๋ฐ›์•„๋“ค์ž…๋‹ˆ๋‹ค.
React๋Š” ๋ณ„๋„์˜ ํŒŒ์ผ์— ๋งˆํฌ์—…๊ณผ ๋กœ์ง์„ ๋„ฃ์–ด ๊ธฐ์ˆ ์„ ์ธ์œ„์ ์œผ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๋Œ€์‹ , ๋‘˜ ๋‹ค ํฌํ•จํ•˜๋Š” โ€œ์ปดํฌ๋„ŒํŠธโ€๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ๋Š์Šจํ•˜๊ฒŒ ์—ฐ๊ฒฐ๋œ ์œ ๋‹›์œผ๋กœ ๊ด€์‹ฌ์‚ฌ๋ฅผ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ดํ›„ ์„น์…˜์—์„œ ๋‹ค์‹œ ์ปดํฌ๋„ŒํŠธ๋กœ ๋Œ์•„์˜ค๊ฒ ์ง€๋งŒ, JS์— ๋งˆํฌ์—…์„ ๋„ฃ๋Š” ๊ฒŒ ์ต์ˆ™ํ•ด์ง€์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ด ์ด์•ผ๊ธฐ๊ฐ€ ํ™•์‹ ์„ ์ค„ ๊ฒƒ์ž…๋‹ˆ๋‹ค

์†์„ฑ ๊ฐ’ ๋„ฃ๋Š” ๋ฐฉ๋ฒ•:

"" ์„ ์ด์šฉํ•ด ๋ฌธ์ž์—ด์„,
{}๋ฅผ ์‚ฌ์šฉํ•ด JavaScript ํ‘œํ˜„์‹ ์‚ฌ์šฉ๊ฐ€๋Šฅ

3๊ฐœ ๋‹ค ๊ฐ™์€ element,

์†์„ฑ(className)์€ props์— ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ
children์— ๋‚ด์šฉ์ด ๋“ค์–ด๊ฐ„๋‹ค

๐Ÿ’ก ์ฃผ์˜ ์œ„ ์•„๋ž˜ ๊ฐ™์€ tag๋กœ ๊ฐ์‹ธ์ ธ์•ผํ•œ๋‹ค.(๊ทœ์น™)


๐Ÿ“‹ ์—˜๋ฆฌ๋จผํŠธ

React ์•ฑ์˜ ๊ฐ€์žฅ ์ž‘์€ ๋‹จ์œ„

<div id="root"></div>

const element = <h1>Hello, world</h1>;
ReactDOM.render(element, document.getElementById('root'));

// root๋ผ๋Š” ID๊ฐ’์„ ๊ฐ€์ง„ ๊ณณ์— element๋ฅผ render()ํ•ด์ค€๋‹ค

React DOM์€ ํ•ด๋‹น ์—˜๋ฆฌ๋จผํŠธ์™€ ๊ทธ ์ž์‹ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ด์ „์˜ ์—˜๋ฆฌ๋จผํŠธ์™€ ๋น„๊ตํ•˜๊ณ  DOM์„ ์›ํ•˜๋Š” ์ƒํƒœ๋กœ ๋งŒ๋“œ๋Š”๋ฐ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋งŒ DOM์„ ์—…๋ฐ์ดํŠธํ•˜์—ฌ ํšจ์œจ์ 

๐Ÿค”์ปดํฌ๋„ŒํŠธ์™€์˜ ์ฐจ์ด?

๊ฐœ๋…์ ์œผ๋กœ ์ปดํฌ๋„ŒํŠธ๋Š” JavaScript ํ•จ์ˆ˜์™€ ๋น„์Šท
โ€œpropsโ€๋ผ๊ณ  ํ•˜๋Š” ์ž„์˜์˜ ์ž…๋ ฅ์„ ๋ฐ›์€ ํ›„, ํ™”๋ฉด์— ์–ด๋–ป๊ฒŒ ํ‘œ์‹œ๋˜๋Š”์ง€๋ฅผ ๊ธฐ์ˆ ํ•˜๋Š” React ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ฐ˜ํ™˜.
element๊ฐ€ ๋” ํ•˜์œ„์˜ ๊ฐœ๋…(ํ™”๋ฉด์— ์–ด๋–ป๊ฒŒ ํ‘œ์‹œ๋˜๋Š”์ง€์˜ ๋Š๋‚Œ)


๐Ÿ“ Function Component, Class Component


Function : props๋ฅผ ๊ฐ€์ง€๊ณ  ๋‹ค๋…€์•ผํ•œ๋‹ค.
Class : extends Component๋ฅผ ํ•˜๊ณ  render()ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ๋‹ค.
์ฐจ์ด์ : : props๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ฌ๋•Œ this ์‚ฌ์šฉํ•˜๋Š”์ง€์˜ ์ฐจ์ด๋„ ์กด์žฌ.

class๋ณด๋‹จ Function ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ์ถ”์„ธ๋ผ๊ณ  ํ•œ๋‹ค~
Why? Life Cycle ์ฐจ์ด์ 
ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ๋Š” props๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ๋ฐœ์ž์˜ ์˜ˆ์ƒ๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ ์ด ์กด์žฌ,
๋ณ„๋„์˜ ์˜ˆ๋ฐฉ ์ž‘์—…์„ ํ•ด ์ฃผ์–ด์•ผ ํ•˜์ง€๋งŒ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋Š” ์ด๋Ÿฌํ•œ ๋ฌธ์ œ์ ์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•œ๋‹ค.
์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋ฐ‘์— ์ถœ์ฒ˜์ฐธ์กฐ.
์ถœ์ฒ˜: https://boxfoxs.tistory.com/395


โญ๏ธ ์ˆœ์ˆ˜ํ•จ์ˆ˜

๋จผ์ € props๋Š” ์ฝ๊ธฐ ์ „์šฉ์ด๋ผ๋Š” ์‚ฌ์‹ค๊ณผ
๋ชจ๋“  React ์ปดํฌ๋„ŒํŠธ๋Š” ์ž์‹ ์˜ props๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ๋ฐ˜๋“œ์‹œ ์ˆœ์ˆ˜ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ๋™์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ผ๋Š” ๊ทœ์น™์ด ์žˆ๋‹ค.
์ˆœ์ˆ˜ํ•จ์ˆ˜๋ž€?
์ž…๋ ฅ๊ฐ’์„ ๋ฐ”๊พธ๋ ค ํ•˜์ง€ ์•Š๊ณ  ํ•ญ์ƒ ๋™์ผํ•œ ์ž…๋ ฅ๊ฐ’์— ๋Œ€ํ•ด ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

์ˆœ์ˆ˜ํ•จ์ˆ˜
function sum(a, b) {
  return a + b; //๋™์ผํ•œ ์ž…๋ ฅ๊ฐ’ (ex, 1,2), ๋™์ผํ•œ ๊ฒฐ๊ณผ (ex return 3 )
} 

์ˆœ์ˆ˜ํ•จ์ˆ˜ X
function withdraw(account, amount) {
  account.total -= amount; // ์ž์‹ ์˜ ์ž…๋ ฅ๊ฐ’์„ ๋ฐ”๊พผ๋‹ค
}

๐Ÿš€ State and Lifecycle

1์ดˆ๋งˆ๋‹ค render๋˜๋Š” ์‹œ๊ณ„๋ฅผ ๋งŒ๋“ค๋ ค๊ณ ํ•œ๋‹ค.

function Clock(props) {
  return (
    <div>
      <h1>Hello, world!</h1>
      <h2>It is {props.date.toLocaleTimeString()}.</h2>
    </div>
  );
}

function tick() {
  ReactDOM.render(
    <Clock date={new Date()} />,
    document.getElementById('root')
  );
}

setInterval(tick, 1000); // 1์ดˆ๋งˆ๋‹ค tick function ํ˜ธ์ถœ

์œ„ ์ฝ”๋“œ์˜ ๋ฌธ์ œ์ :
Clock์ด ํƒ€์ด๋จธ๋ฅผ ์„ค์ •ํ•˜๊ณ  ๋งค์ดˆ UI๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ฒƒ์ด Clock์˜ ๊ตฌํ˜„ ์„ธ๋ถ€์‚ฌํ•ญ์ด ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์ฆ‰, Clock์ด ์Šค์Šค๋กœ ์—…๋ฐ์ดํŠธ๋ฅผ ํ•ด์•ผํ•œ๋‹ค. -->State ์‚ฌ์šฉ

class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = {date: new Date()};
  }

  componentDidMount() { //์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์†Œ๋“œ
    this.timerID = setInterval(
      () => this.tick(),
      1000
    );
  }

  componentWillUnmount() { // ํƒ€์ด๋จธ ๋ฉˆ์ถ”๊ธฐ
    clearInterval(this.timerID);
  }

  tick() {
    this.setState({
      date: new Date() // state์˜ date ๊ฐ’ ๋ณ€๊ฒฝ ์ฝ”๋“œ 
    });
  }

  render() {
    return (
      <div>
        <h1>Hello, world!</h1>
        <h2>It is {this.state.date.toLocaleTimeString()}.</h2> //this.state.date = new Date()
      </div>
    );
  }
}

ReactDOM.render(
  <Clock />,
  document.getElementById('root')
);

๐Ÿ”ฅFunction์—์„œ๋Š” state ์“ฐ๋Š” ๋ฐฉ๋ฒ• hook๊ฐœ๋… ํ•„์š”

class ์ปดํฌ๋„ŒํŠธ์˜ ๊ฒฝ์šฐ State๋ฅผ ๋ฐ”๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค
ํ•˜์ง€๋งŒ Function ์ปดํฌ๋„ŒํŠธ ๊ฒฝ์šฐ Hooks๋ฅผ ์ด์šฉํ•ด์•ผํ•œ๋‹ค.
useState()ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด state ๊ฐ’ ๊ด€๋ฆฌ ๊ฐ€๋Šฅ

์ „์ฒด์ ์ธ ๋ฌธ๋ฒ•


import React, { useState } from 'react'; 
//Hooks๊ฐ€ useState()ํ•จ์ˆ˜ ๋‹ฌ์•„์คŒ


function ์ปดํฌ๋„ŒํŠธ(props) {
  const [๊ฐ’๋ณ€์ˆ˜, ๊ฐ’๋ณ€๊ฒฝํ•จ์ˆ˜] = useState(๊ธฐ๋ณธ๊ฐ’);

  const ๊ฐ’๋ณ€๊ฒฝํ•จ์ˆ˜ = () => {
  //๋ณ€๊ฒฝ๋  ์ฝ”๋“œ
  }
  return(
  	<></>
  );
}

์˜ˆ์ œ ์ฝ”๋“œ

import React, { useState } from 'react'; //Hooks๊ฐ€ useState()ํ•จ์ˆ˜ ๋‹ฌ์•„์คŒ

//+๋ฒ„ํŠผ๊ณผ -๋ฒ„ํŠผ์ƒ์„ฑํ•ด์„œ ์ˆซ์ž๋ฅผ ๋ณ€ํ™”์‹œํ‚ค๋Š” ์ปดํฌ๋„ŒํŠธ
function Counter(props) {
    //num : ์ง€์ •๋  ์ˆซ์ž - ๋ณ€์ˆ˜์— ๋‹ด๊ธฐ
    //setNum : ๋ณ€ํ™”๋  ์ˆซ์ž - ๋ณ€์ˆ˜์— ๋‹ด๊ธฐ
    const [num, setNum] = useState(0); //๊ธฐ๋ณธ๊ฐ’์„ 0์œผ๋กœ ์ฒ˜๋ฆฌ

    //1์”ฉ ์ฆ๊ฐ€ํ•  ํ•จ์ˆ˜
    const onIncrease = () => {
        setNum(num + 1); //ํด๋ž˜์Šค์ปดํฌ๋„ŒํŠธ์˜ setState()ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์‚ฌ์šฉ
    }

    //1์”ฉ ๊ฐ์†Œํ•  ํ•จ์ˆ˜
    const onDecrease = () => {
        setNum(num - 1); //ํด๋ž˜์Šค์ปดํฌ๋„ŒํŠธ์˜ setState()ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์‚ฌ์šฉ
    }
    
    

    return (
        <div style={style}>
            <h1>๊ฐ’๋ณ€ํ™” : { num }</h1>
            <button onClick={onIncrease}>+</button>
            <button onClick={onDecrease}>-</button>
        </div>
    );
}

export default Counter;

๐Ÿ”ฅ ์ฃผ์˜:State ์—…๋ฐ์ดํŠธ๋Š” ๋น„๋™๊ธฐ์ ์ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

// Wrong
this.setState({
  counter: this.state.counter + this.props.increment,
});

// Correct
this.setState(function(state, props) {
  return {
    counter: state.counter + props.increment
  };
});

ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ฒจ์ฃผ๋Š”๊ฒŒ ๋งž๋‹ค.
ํ•จ์ˆ˜๋Š” ์ด์ „ state๋ฅผ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐ›์•„๋“ค์ผ ๊ฒƒ์ด๊ณ , 
์—…๋ฐ์ดํŠธ๊ฐ€ ์ ์šฉ๋œ ์‹œ์ ์˜ props๋ฅผ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐ›์•„๋“ค์ผ ์ˆ˜๋„ ์žˆ๋‹ค.

๐Ÿ˜• Props vs State


๋ฆฌ์•กํŠธ Component์—์„œ ๋‹ค๋ฃจ๋Š” ๋ฐ์ดํ„ฐ๋Š” 2๊ฐœ : Props, State
props ๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ์ฃผ๋Š” ๊ฐ’
์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” props ๋ฅผ ๋ฐ›์•„์˜ค๊ธฐ๋งŒํ•˜๊ณ , ๋ฐ›์•„์˜จ props ๋ฅผ ์ง์ ‘ ์ˆ˜์ • ํ•  ์ˆ˜ ๋Š” ์—†๋‹ค.

state ๋Š” ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ์„ ์–ธํ•˜๋ฉฐ ๋‚ด๋ถ€์—์„œ ๊ฐ’์„ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ.
๐Ÿ’ก ์ฃผ์˜ ๋ณ€๊ฒฝํ• ๋•Œ state = value ํ•˜๋ฉด ์•ˆ๋˜๊ณ  setState = value๋กœ ๋ฐ”๊ฟ”์ฃผ์–ด์•ผ ํ•œ๋‹ค.

์ถœ์ฒ˜: https://velopert.com/3629


๐Ÿ˜ฑ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ

HTML vs React ์ฐจ์ด

HTML
	<button onclick="activateLasers()">
  		Activate Lasers
	</button>
    
    
React
	<button onClick={activateLasers}>
 		Activate Lasers
	</button>
    
HTML
	<form onsubmit="console.log('You clicked submit.'); return false">
  		<button type="submit">Submit</button>
	</form>

React
    function Form() {
	function handleSubmit(e) {
 	e.preventDefault();
        console.log('You clicked submit.');
  	}
 	 return (
    		<form onSubmit={handleSubmit}>
      			<button type="submit">Submit</button>
    		</form>
 	 );
   }
profile
๋ฐฑ์•ค๋“œ๊ฐœ๋ฐœ ๊ธฐ๋ก

0๊ฐœ์˜ ๋Œ“๊ธ€