That React Component Right Under Your Context Provider Should Probably Use React.memo

That React Component Right Under Your Context Provider Should Probably Use React.memo

Leigh Halliday

4 года назад

15,552 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

k ly
k ly - 10.11.2022 19:54

We can provide context to components as a child prop of the provider and not as a direct child.

Ответить
js mastery
js mastery - 01.07.2022 14:58

There will be a problem if you have another state like toggle and setToggle in Context and you import it in Direct child component and then if you change the value of theme in deeper child ,it will re render both direct and deeper child ,even we did not changed the value of toggle in direct child , can you solve that issue ???

Ответить
ThugLifeModafocah
ThugLifeModafocah - 08.06.2022 20:52

Very good video, thanks.

Ответить
Sander Cokart
Sander Cokart - 26.10.2021 02:16

Glad I did the child thing from day 0

Ответить
Sumit Wadhwa
Sumit Wadhwa - 23.05.2021 21:43

Hi. In your first example, if DirectChild and DeeperChild had other components in-between - they would not re-render on toggleTheme only DirectChild and DeeperChild will be re-rendering? right?

Ответить
Mohammad Aliwi
Mohammad Aliwi - 15.05.2021 16:25

What a great video i hope you make more videos about context and how to imporve its performance especially in large apps

Is there any way to avoid the rerendering that happens when you use useContext inside a component

My work flow is like this
I have a global context and global states that have to be shared across many components some of these states arent related to each other and some are
So when i use them inside many component some components rerender themselves when they shouldnt rerender themselves they just rerender themselves because I'm using useContext inside them and using some global states that are inside that context
I hope you understood my issue so far there is no performance issues at all but i was just wondering why is this happening whey these components get rerender every time another state in another component got rerender and the only reason i saw is because i'm using useContext inside them and inside it im pulling out some state from that global context

Ответить
Юрий Яковенко
Юрий Яковенко - 20.04.2021 01:25

react-final-form build on this "Providers -> children" approach

Ответить
Юрий Яковенко
Юрий Яковенко - 20.04.2021 01:24

Thank you!

Ответить
Usman Khalil
Usman Khalil - 27.03.2021 13:52

If someone's confused about the second approach, here's what Dan Abramov has to say about it: "But it still has the same  prop it got from the  last time, so React doesn’t visit that subtree."

Source: Before You memo() — Overreacted

Also, you just earned a subscriber Leigh. You content is gold.

Ответить
Satinder Singh
Satinder Singh - 16.02.2021 04:14

Cool

Ответить
Tajuddin Khan
Tajuddin Khan - 22.01.2021 19:44

Finally got the answer.. Thank you alot

Ответить
Hadi Abedi
Hadi Abedi - 30.10.2020 21:37

Really great video. Thank you.

Ответить
Mark Thrasher
Mark Thrasher - 24.10.2020 03:33

Succinct video that is direct to the point. I enjoy your videos. Thanks for creating them!

Ответить
Zach White
Zach White - 09.10.2020 05:58

Great video!

Ответить
rafael gentil mendes
rafael gentil mendes - 05.10.2020 08:38

Nice vídeo!

Ответить
Malik Brahimi
Malik Brahimi - 31.08.2020 22:51

What if you had dependent contexts like TableContext => RowContext => RowDetailsContext? Would you have to memoize at each level?

Ответить
Hernán B
Hernán B - 07.08.2020 16:26

Good stuff mate. Just a question. Let's say you have a list that you need to order and filter. Would you use context for setting the filters, ordering the list, etc? Thanks mate

Ответить
Kevin Wu Won
Kevin Wu Won - 25.06.2020 18:35

The reason the second fix works: In both cases, App is responsible for rendering DirectChild, so in both cases, DirectChild will re-render when App re-renders. After the refactoring, App is no longer being re-rendered when the state changes; only MyProvider is. Because App no longer needs to be re-rendered, DirectChild does not need to be re-rendered.

Ответить
Alan Medina
Alan Medina - 23.05.2020 20:34

Wow, another awesome video. Thank you very much

Ответить
Abdurrahman Hafez
Abdurrahman Hafez - 17.05.2020 22:17

Your videos are the best

Ответить
xhack89x
xhack89x - 24.03.2020 20:57

I am very happy to have stumbled upon your channel! Excellent content and explanations !! Thanks man!

Ответить
Lotfi Holmes
Lotfi Holmes - 17.03.2020 13:36

Thank you for the explanation!
I was actually using the second method without understanding why 😅

Ответить
Ivan
Ivan - 12.03.2020 19:01

This video earned you a subscription. Great explanation!

Ответить
Tiago Sardinha
Tiago Sardinha - 24.02.2020 13:34

Great explanation! I've been using this pattern (Context + reducer) and so far I'm very satisfied with it.
I just want to leave a question about a use case that might occur:

Imagine that instead of one DeeperChild, you have a list of them. Any change of the context's state triggered by one of those components will trigger a re-render in the remaining ones. Do you see any way of avoiding this or you wouldn't care about it?

Best regards

Ответить
Einar Paul Qvale
Einar Paul Qvale - 24.02.2020 11:26

Nice video, thanks. A couple of comments:
1. The trick with not needing to memioze if using the "children" prop was new to me. This needs some further digging, I feel like something might be missing here. Is this documented somewhere?
2. Perhaps you should also memoize the context value? In this case the context value is always going to be a new object, but since you are creating the context value in App (which is never re-rendered) then it's not a problem after all. If this theme provider was also rendered further down the tree, it would probably re-create the context value too often and hence invalidate the children too often as well.

Ответить
Pavel Ravits
Pavel Ravits - 21.02.2020 23:47

Would you explain please, why with {children} it doesn't do the unnecessary rerender

Ответить
Shak Daniel
Shak Daniel - 19.02.2020 05:55

I like that you always make your videos short and informative.

Ответить
Zachary Edwards
Zachary Edwards - 18.02.2020 21:03

good shit!

Ответить
swyx
swyx - 18.02.2020 19:32

excellent explanation.

Ответить
MoT - Master of Things
MoT - Master of Things - 18.02.2020 19:26

Now you have video quality greater than 360p, thanks for that.

Ответить
Pouria Taj
Pouria Taj - 18.02.2020 19:22

Great job sir, there is few content creators like you who dive deep into JS stuff... I'm always enjoyed watching your videos hopefully you never stop that . BestRegards

Ответить
Janto Motulo
Janto Motulo - 18.02.2020 19:16

i have to wrap one level top component with react.memo before component that will useContext?? sorry if my english is bad heheh

Ответить
Phan Nam
Phan Nam - 18.02.2020 18:02

Hello

Ответить