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

refactor(toast): Fix unnecessarily re-rendering all children of toast context on every state change #197

Merged
merged 4 commits into from
Jun 28, 2022

Conversation

jamcry
Copy link
Collaborator

@jamcry jamcry commented Jun 23, 2022

What is the problem?

  • In our apps, we mostly only care Toast state dispatch function. But in the current implementation, since useToaster returns both state and dispatch in the same object, dispatch function will "change" on every state change, and this will cause re-render in the components that use dispatch.

Possible (implemented) Solution

  • Separating state and dispatch contexts, so that state changes won't trigger a change in dispatch

Result (before vs after) [green border means that element is re-rendered]

I tried on my local with npm-link, here is the result:
https://user-images.githubusercontent.com/22727931/175345592-35eadfd1-6b05-4a3d-9334-7320a0e95f32.mov
Only necessary elements re-renders. As expected.

If the app is only using useToaster, it won't be a breaking change and won't require any changes on the project

dependabot bot and others added 2 commits April 7, 2022 14:18
Bumps [minimist](https://github.com/substack/minimist) from 1.2.5 to 1.2.6.
- [Release notes](https://github.com/substack/minimist/releases)
- [Commits](https://github.com/substack/minimist/compare/1.2.5...1.2.6)

---
updated-dependencies:
- dependency-name: minimist
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>
Copy link
Contributor

@edizcelik edizcelik left a comment

Choose a reason for hiding this comment

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

This approach is the way to go when using Contexts 🥇 I had a few comments about how we can make the consumers safer

@edizcelik
Copy link
Contributor

By the way, we can update the file structure here. We recently started using this convention on Tinyman and it looks quite nice:

.../ToastContext.tsx     <- this includes Contexts and the ToastContextProvider
.../ToastContext.utils.ts      <-  this includes the custom hooks for the consumers
.../ToastContext.reducer.ts      <- includes the reducer and the state stuff
.../ToastContext.constants.ts    <- includes constants related to the Context
.../ToastContext.types.ts      <- includes types related to the Context (if any)

@jamcry
Copy link
Collaborator Author

jamcry commented Jun 24, 2022

By the way, we can update the file structure here. We recently started using this convention on Tinyman and it looks quite nice:

.../ToastContext.tsx     <- this includes Contexts and the ToastContextProvider
.../ToastContext.utils.ts      <-  this includes the custom hooks for the consumers
.../ToastContext.reducer.ts      <- includes the reducer and the state stuff
.../ToastContext.constants.ts    <- includes constants related to the Context
.../ToastContext.types.ts      <- includes types related to the Context (if any)

I think it's a good idea to make these files more organized 👍
It will cause changes at a lot of files because toast has three contexts now: ToastDispatchContext, ToastStateContext and ToastItemContext.

I'll create an issue, we can do this refactor in another PR, also for ToggleContext to make all consistent

@jamcry jamcry marked this pull request as ready for review June 24, 2022 08:57
@jamcry jamcry requested a review from edizcelik June 24, 2022 08:58
@jamcry jamcry added Ready for new release PR is reviewed and ready to be merged and removed ready for review labels Jun 28, 2022
@jamcry jamcry changed the base branch from main to next-release June 28, 2022 13:09
@jamcry jamcry merged commit 3ba2a04 into next-release Jun 28, 2022
@jamcry jamcry deleted the hotfix/toast-context-splitting branch June 28, 2022 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ready for new release PR is reviewed and ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants