Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Performance: don't put location updates in React context #324

Closed
gmaclennan opened this issue Feb 28, 2020 · 0 comments
Closed

Performance: don't put location updates in React context #324

gmaclennan opened this issue Feb 28, 2020 · 0 comments

Comments

@gmaclennan
Copy link
Member

Change the location provider to pass an event emitter, instead of a frequently updating value. This is because of the performance costs in React of frequent updates on context.

Background

Currently we store location (from the GPS) in React context. We do this so that the location is available in different screens throughout the app without needing to pass it down as a prop. We use context for this because it is the recommended approach for React Navigation, the routing library we use - because of the way mobile works with different screens, context is the easiest way to make a single source of data available to multiple screens.

The way context works, only components that subscribe to the context will re-render when the context value updates. This is good, it minimizes the number of re-renders, which are costly, especially on low-powered devices. However, context updates are not "free" - every time context updates React traverses the whole tree to check for components which are subscribing to the context. For a frequently updating value (like GPS status) this has a performance hit. The authors of Redux discovered this limitation and one of the React maintainers confirms that context is not suitable for frequently updating values.

The solution to this is to pass around an event emitter on context — the actual reference to the event emitter will not change, so there is no reconciliation of re-renders. Any component which needs the location can access the location event emitter from context, and subscribe to updates. The best way to do this is with useSubscription although in the future useMutableSource will be the better solution.

@digidem digidem locked and limited conversation to collaborators Oct 1, 2021
@ErikSin ErikSin closed this as completed Oct 1, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

2 participants