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

[7.x] [APM] Make sure URL hooks are in sync with history.location (#36676) #37158

Merged
merged 1 commit into from
May 25, 2019

Conversation

dgieselaar
Copy link
Member

Backports the following commits to 7.x:

…36676)

* [APM] Make sure URL hooks are in sync with history.location

Previously, the `useUrlParams` and `useLocation` hooks were possibly running out of sync with history.location, as they maintained their own state. By listening to history changes, they would re-render their context providers with the updated values. However, the context providers are wrapped by the `Router` component which re-renders the context providers when the location changes. This means that the initial render after a location change would have the updated values in `history.location`, but not in the values returned from the `useUrlParams` and `useLocation` hooks. These hooks would then (both) queue another render with the updated values being passed to the context.

This change lets the hooks piggyback on the re-renders from the `Router` component, and uses `history.location` to pass their derived values to the context. This ensures that the values returned from the hooks and `history.location` are always in sync. It should also remove a few unnecessary renders.

Fix tests

- Passes history as a prop to URLParamsProvider, easier to test and consistent with usage of LocationProvider
- Do not test behaviour of history.replace & re-renders, only whether history.replace was called with the right Location values. This is because re-rendering is no longer a concern of URLParamsProvider, we leave that up to the Router component now.

Address review feedback:

- Use history/location from Router so we have one source of truth for routing info
- For UrlParamsContext, avoid use of history.replace. The behaviour w/ history.replace is incorrect. We now use useRef and trick React into re-rendering with an "empty" state update.
- Unit test for whether useUrlParams does not queue an unnecessary render.

Memoize context value of UrlParamsProvider

* Move infra items back to the top again

Piggybacking on this change to fix an order issue in TransactionActionMenu that was introduced w/ 2e3fbd2 (woops).
@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@dgieselaar dgieselaar merged commit 72060b1 into elastic:7.x May 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants