Recent Posts
Recent Comments
Link
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

kohigowild

[React] props์™€ state ๋ณธ๋ฌธ

React

[React] props์™€ state

kohi โ˜• 2022. 12. 15. 23:52

๐Ÿ‘ฝ props์™€ state


๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋‹ค๋ฃจ๋Š” ๋ฐ์ดํ„ฐ๋Š” ๋‘ ๊ฐœ๋กœ ๋‚˜๋‰œ๋‹ค. ๋ฐ”๋กœ props์™€ state์ธ๋ฐ, props๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ์ฃผ๋Š” ๊ฐ’์ด๋‹ค. ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” props๋ฅผ ๋ฐ›์•„์˜ค๊ธฐ๋งŒ ํ•˜๊ณ , ๋ฐ›์•„์˜จ props๋ฅผ ์ง์ ‘ ์ˆ˜์ •ํ•  ์ˆ˜๋Š” ์—†๋‹ค. ๋ฐ˜๋ฉด์— state๋Š” ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ์„ ์–ธํ•˜๋ฉฐ ๋‚ด๋ถ€์—์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ’ก props

props๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ถ€๋ฅผ ๋•Œ ํ•จ๊ป˜ ์ง€์ •ํ•œ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์—์„œ๋Š” People ์ปดํฌ๋„ŒํŠธ์— name prop๊ณผ age prop์„ ์ง€์ •ํ•˜์˜€๋‹ค.

<People name="mitt" age={ 28 } />
<People name="rachel" age={ 27 } />
  • ์œ„์™€ ๊ฐ™์ด ๊ฐ™์€ ํƒ€์ž…์˜ ์ปดํฌ๋„ŒํŠธ์— ๋‹ค๋ฅธ props ๊ฐ’์„ ์ฃผ์–ด ํŒจํ„ด์ด ๋ฐ˜๋ณต๋˜๋Š” ํ˜•ํƒœ๋กœ ์ปดํฌ๋„ŒํŠธ์˜ ํšจ์œจ์ ์ธ ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • props์—๋Š” ๋ถˆ๋ฆฌ์–ธ, ์ˆซ์ž, ๋ฐฐ์—ด๊ณผ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์„ ์ˆ˜ ์žˆ๊ณ , ๊ณต๋ฐฑ ๊ตฌ๋ถ„์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ๋‹ด๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. props์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋Š” ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๋ฉด ๋ชจ๋‘ ์ค‘๊ด„ํ˜ธ๋กœ ๊ฐ’์„ ๊ฐ์‹ธ์•ผ ํ•œ๋‹ค.

 

๐Ÿ’ก props ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ

  • props๋Š” ์ฝ๊ธฐ ์ „์šฉ์ด๋ฏ€๋กœ ์ปดํฌ๋„ŒํŠธ์˜ ๋‚ด๋ถ€์—์„œ props๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋‹ค.
  • props๋ฅผ ๋ฐ›๋Š” ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์— ์ธ์ž๋ฅผ ์ •์˜ํ•˜๋ฉด, props๋ฅผ ์†์„ฑ์œผ๋กœ ๊ฐ€์ง€๋Š” ๊ฐ์ฒด Object๊ฐ€ ํ•ด๋‹น ์ธ์ž๋กœ ์ „๋‹ฌ๋œ๋‹ค. ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉ ์‹œ, ๊ฐ์ฒด์— ์กด์žฌํ•˜๋Š” ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋“ฏ ์  ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›ํ•˜๋Š” props๋ฅผ ๊บผ๋‚ด ์“ธ ์ˆ˜ ์žˆ๊ณ , ์ด๋ฅผ ์ค‘๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ๋Š” ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
function People(props) {
	return {
		<div>{props.name}</div>
		<div>{props.age}</div>
	}
}

 

๐Ÿ’ก state

  • state๋Š” ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์˜ ๋™์  ๋ฐ์ดํ„ฐ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. state๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์—๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.
    • ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ์ปดํฌ๋„ŒํŠธ ์ž์ฒด๊ฐ€ state๋ฅผ ์ง€๋‹ˆ๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
    • ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” useState๋ผ๋Š” ํ•จ์ˆ˜, Hook์„ ํ†ตํ•ด ์‚ฌ์šฉํ•œ๋‹ค.
  • ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž์‹์œผ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ์œผ๊ฑฐ๋‚˜ ๋‘ ๊ฐœ์˜ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋“ค์ด ์„œ๋กœ ํ†ต์‹ ํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์กฐ์ƒ ์ปดํฌ๋„ŒํŠธ์— state๋ฅผ ์ •์˜ํ•œ๋‹ค. ์กฐ์ƒ ์ปดํฌ๋„ŒํŠธ๊ฐ€ props๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์— state๋ฅผ ๋‹ค์‹œ ์ „๋‹ฌํ•จ์œผ๋กœ์จ ์„œ๋กœ ๋™๊ธฐํ™”ํ•œ๋‹ค.

 

๐Ÿ’ก ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ state ์‚ฌ์šฉ

ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ state๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๊ฐ์ฒด ํ˜•์‹์˜ this.state๋ฅผ ํ†ตํ•ด state ๊ฐ์ฒด์˜ ์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•˜๊ณ  ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. state ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ๊ฒฝ์šฐ this.setState๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ค€๋‹ค.

class ClassExample extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0,
    };
  }

  render() {
    const { count } = this.state;
      return (
        <div>
          <p>You clicked {count} times</p>
          <button onClick={() => {
            this.setState({
              count: count + 1
            });
          }}>
            Click me
          </button>
        </div>
      );
    }
  }
}

 

๐Ÿ’ก ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ state ์‚ฌ์šฉ

useState()

  • ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋™์ ์ธ ๊ฐ’์„ ์ƒํƒœ(state)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์‚ฌ์šฉ์ž ์ธํ„ฐ๋ž™์…˜์„ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ๊ฐ’์ด ๋™์ ์œผ๋กœ ๋ฐ”๋€” ๊ฒฝ์šฐ์—๋Š” ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค.
  • Hooks ๊ธฐ๋Šฅ์ด ๋„์ž…๋œ ๋ฆฌ์•กํŠธ 16.8 ๋ฒ„์ „๋ถ€ํ„ฐ useState() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋„ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • https://trustmitt.tistory.com/41
 

React useState(), useEffect()

๐Ÿšฉ useState() ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋™์ ์ธ ๊ฐ’์„ ์ƒํƒœ(state)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์‚ฌ์šฉ์ž ์ธํ„ฐ๋ž™์…˜์„ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ๊ฐ’์ด ๋™์ ์œผ๋กœ ๋ฐ”๋€” ๊ฒฝ์šฐ์—๋Š” ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค. Hooks ๊ธฐ๋Šฅ์ด ๋„์ž…

trustmitt.tistory.com

 

๐Ÿ‘ฝ props์™€ state EX


๐Ÿ’ก props

<div id="name" class="label" onclick="alert('Hello World!');">
  Hello world!
</div>

์œ„์˜ ์ฝ”๋“œ๋Š” HTML์˜ div ํƒœ๊ทธ์— id์™€ class ์†์„ฑ์„ ์„ค์ •ํ•˜๊ณ  onclick ์†์„ฑ์— ์ง์ ‘ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ alert๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. 

 

const Text = ({text}) => {
  return <div>{text}</div>
}

const App = () => {
  return <Text text='Hello world!'/>
}

์œ„์™€ ๊ฐ™์ด ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ(App)์—์„œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ(Text)์— ์†์„ฑ(Props)์„ ์ด์šฉํ•˜์—ฌ Hello world! ๋ผ๋Š” ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ’ก state

import React, { useState } from 'react';

const Text = ({text}) => {
  return <div>{text}</div>
}

const App = () => {
  const [count, setCount] = useState(0);

  return <div>
    <Text text={count} />
    <div onClick={() => setCount(count + 1)}>+</div>
  </div>
}

 

๐Ÿ‘ฝ Reference


https://reactjs.org/docs/components-and-props.html

 

Components and Props – React

A JavaScript library for building user interfaces

reactjs.org

https://reactjs.org/docs/state-and-lifecycle.html

 

State and Lifecycle – React

A JavaScript library for building user interfaces

reactjs.org

https://velog.io/@soyi47/React-Component-props-state

 

[React] Component์™€ props, state

๐Ÿš€ Component, props, state์— ๋Œ€ํ•ด ์•Œ์•„๋ณธ๋‹ค.

velog.io

https://studyingych.tistory.com/52

 

React Props, State ์ดํ•ด ๋ฐ ์‚ฌ์šฉ๋ฒ•

Props / State props ์™€ state๋Š” React์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋‹ค๋ฃจ๋Š” ๊ฐœ๋…์ด๋‹ค. ์‹ค์Šต ์ฝ”๋“œ์ƒŒ๋“œ๋ฐ•์Šค(https://codesandbox.io/s/dank-pond-pfyvq) ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹ค์Šต Props props๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์— ๊ฐ’์„

studyingych.tistory.com

https://velopert.com/3629

 

๋ˆ„๊ตฌ๋“ ์ง€ ํ•˜๋Š” ๋ฆฌ์•กํŠธ 4ํŽธ: props ์™€ state | VELOPERT.LOG

์ด ํŠœํ† ๋ฆฌ์–ผ์€ 10ํŽธ์œผ๋กœ ์ด๋ค„์ง„ ์‹œ๋ฆฌ์ฆˆ์ž…๋‹ˆ๋‹ค. ์ด์ „ / ๋‹ค์Œ ํŽธ์„ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด ๋ชฉ์ฐจ๋ฅผ ํ™•์ธํ•˜์„ธ์š”. ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋‹ค๋ฃจ๋Š” ๋ฐ์ดํ„ฐ๋Š” ๋‘๊ฐœ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ๋ฐ”๋กœ props ์™€ state ์ธ๋ฐ์š”, ๋ฏธ๋ฆฌ ์š”์•ฝ

velopert.com