This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Performance: don't put location updates in React context #324
Labels
You can continue the conversation there. Go to discussion →
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 futureuseMutableSource
will be the better solution.The text was updated successfully, but these errors were encountered: