리액트 컨텍스트 또는 Redux? 리렌더링(성능)의 차이점은 무엇입니까?
목차
🗽 How to share a state
Share a state by Props
Share a state by React Context
Share a state by Redux
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.orghttps://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. 🤷
📖 독서
Context - ReactRedux - A predictable state container for JavaScript apps. | Redux
How to destroy your app performance using React contexts
저자 앤서니 고딘, .
Reference
이 문제에 관하여(리액트 컨텍스트 또는 Redux? 리렌더링(성능)의 차이점은 무엇입니까?), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/anthogdn/react-context-or-redux-whats-difference-about-rerendering-performance-3kck텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)