๋ฆฌ์•กํŠธ ์ƒํƒœ๊ด€๋ฆฌ - props & Context API

1. ํด๋ผ์ด์–ธํŠธ ์ƒํƒœ๊ด€๋ฆฌ (State management) ?

  • SPAs๊ตฌ์กฐ์—์„œ๋Š” ๋‹ค์–‘ํ•œ ํ™”๋ฉด ๋ Œ๋”๋ง์ด ํ•„์š”ํ•จ.

  • ํ™”๋ฉด ๋ Œ๋”๋ง์€ ๋‹ค์–‘ํ•œ state ๋ณ€๊ฒฝ์— ์˜ํ•ด์„œ ๋ฐœ์ƒ.

  • ๋‹ค์ˆ˜์˜ state๊ฐ€ ์žˆ์œผ๋ฉฐ ๊ฐ state๋Š” ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๊ณ , ์–ด๋–ค state๋Š” ์„œ๋ฒ„์™€ ๋™๊ธฐํ™” ๋ผ์•ผ ํ•œ๋‹ค.

2. React state management

component block state

  • ํ•˜๋‚˜์˜ ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ์˜ ๋ณ€๊ฒฝ

  • ์ง€์—ญ๋ณ€์ˆ˜์˜ ๋ณ€๊ฒฝ๊ณผ ๋น„์Šท

  •   const [data, setData] = useState("");
    

component sharing state

์ปดํฌ๋„ŒํŠธ ๊ฐ„์— ์ƒํƒœ๋ฅผ ๊ณต์œ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ณ , ๋Œ€๋ถ€๋ถ„์€ global state ์ˆ˜์ค€์œผ๋กœ ๊ด€๋ฆฌํ•œ๋‹ค.

  • ์ƒํƒœ๋ฅผ ์ž์‹์—๊ฒŒ ์ „๋‹ฌ

    • props ๋กœ ์ „๋‹ฌ

    • ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋Š” props์†์„ฑ์„ ํ†ตํ•ด์„œ ํ•„์š”ํ•œ ์ƒํƒœ๋ฅผ ๋ฐ›์•„์„œ ๋ Œ๋”๋ง

  • ์ƒํƒœ๋ฅผ ์ž์‹์ด ์•„๋‹Œ ๋‹ค๋ฅธ node ๊ณ„์ธต์— ์ „๋‹ฌ

    • ์ƒ์œ„ ์–ด๋”˜๊ฐ€ state๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ปดํฌ๋„ŒํŠธ๊ฐ„์˜ ๊ณต์œ ๊ฐ€ ๋˜๋„๋ก ํ•˜๊ธฐ.

      • ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ๊ณตํ†ต์˜ ๋ถ€๋ชจ ์˜์—ญ์— state๋ฅผ ๋งŒ๋“ค๊ณ , ๊ฐ๊ฐ state๋ฅผ ๋‚ด๋ ค๋ฐ›์•„์„œ ํ™œ์šฉ.


        Image:ย https://user.oc-static.com/upload/2021/05/06/16202866148021_P3C3-2.png

      • props ๋ฅผ ํ†ตํ•ด์„œ ๊ณ„์† ์ „๋‹ฌํ•ด์ค˜์•ผ ํ•˜๋Š” ๋‹จ์  ์กด์žฌ (props drilling)

Props drilling ๋ฌธ์ œ ํ•ด๊ฒฐ: Context API ์‚ฌ์šฉ

  • ์™ผ์ชฝ์€ Props drilling, ์˜ค๋ฅธ์ชฝ์€ย Context API ์‚ฌ์šฉ

  • Provider ์™€ Consumer ๊ฐœ๋…์œผ๋กœ ์ œ๊ณต/์†Œ๋น„ ํ•˜๋Š” ๊ฐœ๋…

  • props ๋ฅผ ๊ณ„์† ์ „๋‹ฌํ•ด์ค„ ํ•„์š”๊ฐ€ ์—†์Œ.

  • React ๊ฐ€ ์ปดํฌ๋„ŒํŠธ ๊ณ„์ธต์„ ๋ถ„์„ํ•ด์„œ props๋กœ ์ „๋‹ฌ์‹œํ‚ด

image:ย https://miro.medium.com/max/2000/1*Ha2vNB0ILaYKPXk6oyTZSQ.png

์˜ˆ์‹œ.

https://milddev.com/global-state-management-with-react-usestate-usecontext-hooks-and-context-api

์ฐธ๊ณ . React-Redux ์—์„œ๋„ Context API๋ฅผ ์‚ฌ์šฉํ•œ ๋ฐฉ์‹

import { createStore} from 'redux';
import { Provider } from 'react-redux';

//Provider
const store = createStore(rootReducer)
ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
)


//Consumer
import React from 'react'
import { useSelector } from 'react-redux'

export const CounterComponent = () => {
  const counter = useSelector((state) => state.counter)
  return <div>{counter}</div>
}


image:ย https://css-tricks.com/wp-content/uploads/2016/03/redux-article-3-03.svg?w=640

Props drilling ๋ฌธ์ œ ํ•ด๊ฒฐ: Component composition

Composition ?

https://reactjs.org/docs/composition-vs-inheritance.html

Ex) ์ผ๋ฐ˜์ ์ธ ์ฝ”๋“œ

์ฐธ๊ณ  :ย https://javascript.plainenglish.io/how-to-avoid-prop-drilling-in-react-using-component-composition-c42adfcdde1b


export default function App() {
  return (
    <div className="App">
      <FirstComponent content="Who needs me?" />
    </div>
  );
}

function FirstComponent({ content }) {
  return (
    <div>
      <h3>I am the first component</h3>;
      <SecondComponent content={content} />|
    </div>
  );
}

function SecondComponent({ content }) {
  return (
    <div>
      <h3>I am the second component</h3>;
      <ThirdComponent content={content} />
    </div>
  );
}

function ThirdComponent({ content }) {
  return (
    <div>
      <h3>I am the third component</h3>;
      <ComponentNeedingProps content={content} />
    </div>
  );
}

function ComponentNeedingProps({ content }) {
  return <h3>{content}</h3>;
}

๊ฐœ์„ 

function FirstComponent({ children }) {
  return (
    <div>
      <h3>I am the first component</h3>;
     { children }
    </div>
  );
}

function SecondComponent({ children }) {
  return (
    <div>
      <h3>I am the second component</h3>;
     {children}
    </div>
  );
}

function ThirdComponent({ children }) {
  return (
    <div>
      <h3>I am the third component</h3>
        {children}
    </div>
  );
}

function ComponentNeedingProps({ content }) {
  return <h3>{content}</h3>
}

export default function App() {
  const content = "Who needs me?";
 return (
    <div className="App">
      <FirstComponent>
        <SecondComponent>
          <ThirdComponent>
            <ComponentNeedingProps content={content}  />
          </ThirdComponent>
        </SecondComponent>
      </FirstComponent>
    </div>
  );
}