리액트 컨텍스트 또는 Redux? 리렌더링(성능)의 차이점은 무엇입니까?

목차


  • 🎯 Purpose
  • 🧸 Demo app

  • 🗽 How to share a state

  • Share a state by Props
  • Illustration


  • Share a state by React Context
  • Illustration


  • Share a state by Redux
  • Illustration

  • Share a state with other tools like Mobx, Recoil etc

  • 🎦 Rendering demonstration vidéo - Props vs React Context vs Redux
  • ❓So?
  • 🧑‍🚀 Further
  • 📖 Some Reading



  • The purpose is to develop some ideas in order to provoke a thinking for readers and for myself.
    Happy reading.




    🎯 목적

    What's the difference between React Context and Redux according to performance?

    The question "Which one is the best to use for a better productivity/code?" is an other question that we don't looking for an answer here.

    🧸 데모 앱

    To illustrate this article, I created a github project that you can clone and execute on your own computer.

    You can check it out if you want but it's not necessary to understand this article.

    https://github.com/AnthoGdn/labo_redux_vs_react-context

    🗽 상태를 공유하는 방법

    With React, we have some possibilities to share a state into all components of the application. We can use Props, React Context, Redux, Mobx, Recoil etc...

    You can find some code examples on my github project. (shared above)

    Props로 상태 공유

    The idea is to create a state into a component (we name it " common ancestor" here) and share it to its children. We need to share "setter functions" too.

    const [text, setText] = useState('')
    // setText is a setter.
    

    Every times we use a setter to change state, it's going to change the state into all common ancestor component, so it's going to rerender (paints again view) ancestor component and all its children.

    ⚠️ It could be bad for performance if the page contains heavy graphic elements and graphic computations.

    삽화



    React Context로 상태 공유

    React Context is exactly the same that Props. It's just syntactic sugar.

    Every times we modify state into common ancestor component, it's rerender common ancestor component and all its children.

    ⚠️ It could be bad for performance if the page contains heavy graphic elements and graphic computations.

    삽화



    Redux로 상태 공유

    We branch a state to an ancestor component. React will not rerender common ancestor. It will rerender only component which use the part of the state that change.

    ⚠️ The behaviour is not the same that React Context.

    삽화



    Mobx, Recoil 등과 같은 다른 도구와 상태 공유

    I've never used these tools. But I think it's the same rendering behaviour than Redux. 🤷

    https://recoiljs.org
    https://mobx.js.org

    🎦 시연 동영상 렌더링 - Props vs React Context vs Redux

    We can see the result of the 3 rerendering when we change the state with props, React Context and Redux.
    You can see that with Redux, component 1 and 3 are not rerendering when we change state.

    ❓그래서?

    People asks me sometimes witch one is better to use. What's the best?

    I think there is no best. It depends of the context of your application.

    Now, you know what's the behaviour of these tools on the rendering of your components.

    According of the context of your application, it's possible that one of these tools be better than the other.

    • If your state will not change often in the user navigation, so you can use what you want (rendering thinking).
    • If your state will change often, it could be better to use Redux, specially if your "ancestor component" contains heavy graphic elements. For example, it's probably not a good idea to use React Context in a messaging or websocket or Server-Sent Events, because state could change a lot in short time. 🤷

    Don't forget you can use React Context and Redux in the same application. Some people can say it's not recommended to use both in same application but why not? Using Redux for specific business logic of your product and using React Context for an other. Why not because React Context is included in React and it's just syntactic sugar of props. It costs no more to use it if you want to. 🤷

    Tips: It's probably not a big deal sometimes to rerender components for nothing with React Context. The most important that users don't feel it. Don't over engineer is you don't need to.

    Again:
    The question "Which one is the best to use for a better productivity/code?" is another question that we don't looking for an answer here.

    🧑‍🚀 더 나아가

  • This point of view is theoretical. I didn't measure performance and demonstrate performance problem with React Context. It could be a purpose for a next article why not. 🤷

  • Also, it's possible to use React Memo 쓸모없는 재 렌더링을 피하기 위해 React Context를 사용하지만 Redux보다 사용하는 것이 지칠 수 있다고 생각하지 않습니까? 🤷
  • 더 이상 전역 상태를 사용하여 http 응답 데이터를 저장할 필요가 없습니다. 이를 위해 데이터를 가져오고 캐시하고 업데이트하는 라이브러리가 있습니다. React Query 그것을 위해 훌륭합니다. 또는 Apollo client Graphql http 요청의 경우.

  • 📖 독서

    Context - React

    Redux - A predictable state container for JavaScript apps. | Redux

    How to destroy your app performance using React contexts


    저자 앤서니 고딘, .

    좋은 웹페이지 즐겨찾기