Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEI-5465.4: Lesson 2 - Prevent Context From Rerendering #4

Merged
merged 2 commits into from
Mar 25, 2024

Conversation

kevinbarabash
Copy link
Contributor

@kevinbarabash kevinbarabash commented Feb 23, 2024

Summary:

Lesson 2 shows how to minimize context induced re-renders by having the context store an instance of an event emitter that never gets replaced. Changes to the state in the context are now effected by emitting event and listening for them.

Issue: FEI-5465

Test plan:

exercise
Screen Shot 2024-02-26 at 11 27 40 AM

solution
Screen Shot 2024-02-26 at 11 27 37 AM

@kevinbarabash kevinbarabash self-assigned this Feb 23, 2024
@kevinbarabash kevinbarabash marked this pull request as draft February 23, 2024 22:31
@kevinbarabash kevinbarabash changed the title FEI-5465.4: Lesson 2 - Prevent Context From Rerendering [WIP] FEI-5465.4: Lesson 2 - Prevent Context From Rerendering Feb 26, 2024
@kevinbarabash kevinbarabash marked this pull request as ready for review February 26, 2024 16:11
Copy link

@jaredly jaredly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

Comment on lines +28 to +36
const Foo = () => {
const {foo} = useContext(FooBarContext);
return <h1>foo = {foo}</h1>;
};

const Bar = () => {
const {bar} = useContext(FooBarContext);
return <h1>bar = {bar}</h1>;
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm this example is confusing to me, because in fact both child components are using the context. I think the point would be clearer if Bar didn't use the context at all, but did some expensive render-time computation (and had a Date.now() rendered for good measure).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good feedback. I'll update the example. Is the Date.now() to show when it was last rendered so its easier for people to see when a component isn't being rendered?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example never gets rendered so I'm not sure how useful the Date.now() would be.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah, if it's not actually run then the date.now isn't needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the expensive render-time computation case is covered by Lesson #1. This particular lesson is showing how using Context can often result in unnecessary renders.

@kevinbarabash kevinbarabash changed the base branch from fe-5465.3 to main February 27, 2024 15:59
@kevinbarabash kevinbarabash merged commit 122db84 into main Mar 25, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants