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

[Logs UI] Fix errors during navigation #78319

Merged
merged 9 commits into from
Nov 9, 2020

Conversation

afgomez
Copy link
Contributor

@afgomez afgomez commented Sep 23, 2020

Summary

Closes #69854.

Avoid setting state when the container hooks are not mounted.

React warns if a component tries to call useState or dispatch after a hook has been unmounted. This might happen if the call to any of those methods happens as a result of an async operation finishing after a component has been unmounted.

To solve this, we check if the component is mounted before trying to set the state or dispatch actions.

How to reproduce in master (and test for correctness in the branch)

  • Open your browser's devtools, in the console tab.
  • Go to the logs app.
  • While the entries are loading, go somewhere else.
  • A warning should show up in the console.

console warning

  • While in this branch, nothing should show up in the console.

@afgomez afgomez added v8.0.0 Feature:Logs UI Logs UI feature Team:Infra Monitoring UI - DEPRECATED DEPRECATED - Label for the Infra Monitoring UI team. Use Team:obs-ux-infra_services release_note:skip Skip the PR/issue when compiling release notes v7.10.0 labels Sep 23, 2020
@afgomez afgomez added this to the Logs UI 7.10 milestone Sep 23, 2020
@afgomez afgomez self-assigned this Sep 23, 2020
@weltenwort weltenwort added the bug Fixes for quality problems that affect the customer experience label Oct 12, 2020
@sgrodzicki sgrodzicki modified the milestones: Logs UI 7.10, Logs UI 7.11 Oct 12, 2020
@afgomez afgomez marked this pull request as ready for review October 13, 2020 10:29
@afgomez afgomez requested a review from a team as a code owner October 13, 2020 10:29
@elasticmachine
Copy link
Contributor

Pinging @elastic/logs-metrics-ui (Team:logs-metrics-ui)

@Kerry350 Kerry350 self-requested a review October 13, 2020 16:41
Copy link
Contributor

@Kerry350 Kerry350 left a comment

Choose a reason for hiding this comment

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

Fixes the issue 👍

Question on the implementation: Would it have been possible to handle this in useTrackedPromise alone, rather than adding the isMounted awareness and onReject callbacks to all the individual hooks?

E.g. where we have:

if (onResolve) {
  onResolve(value);
}

in useTrackedPromise could that have been:

if (onResolve && !isMounted) {
  onResolve(value);
}

? useTrackedPromise is already "aware" of mounted vs unmounted for the useEffect.

Anyway, just curious, as this way does mean we need to add extra bits when we use the hook, but if there's an explicit reason then ignore this 😄

@weltenwort
Copy link
Member

@Kerry350 I've almost suggested this as well. It makes sense to do that if we assume the onResolve and onReject handlers will always do something that is sensitive to the React component lifecycle. Looking at the current usage this is mostly the case, but it doesn't have to. It could become an option one could opt out of?

@afgomez
Copy link
Contributor Author

afgomez commented Oct 15, 2020

Would it have been possible to handle this in useTrackedPromise alone, rather than adding the isMounted awareness and onReject callbacks to all the individual hooks?

That's a good question. My reasoning to not handle it in useTrackedPromise is that it may or may not matter if the component is mounted or not.

Consider this example:

function SomeComponent() {
  const [_, createPromise] = useTrackedPromise({
	createPromise: () => Promise.resolve(),
    onResolve: () => { console.log("will I be called?") },
  });
  
  useLayoutEffect(() => { createPromise() });

  return null;
}

useMounted() internally uses useEffect(). This example uses useLayoutEffect(), which is called before the component renders.

If we call useMounted() inside useTrackedPromise() and use it as a dependency inside, the callback in the example might never be called (or maybe yes! I'm not sure how React schedules these things).

This was my reasoning for it, but maybe this will never be a problem. Right now we are not using useLayoutEffect() anywhere

~/code/kibana[master]% git grep useLayoutEffect -- x-pack/plugins/infra | wc -l
       0

...and we might never will 🤷, but it felt more right to move the responsibility of checking for "mountedness" to the components themselves.

@afgomez
Copy link
Contributor Author

afgomez commented Oct 15, 2020

@elasticmachine merge upstream

@Kerry350
Copy link
Contributor

but it felt more right to move the responsibility of checking for "mountedness" to the components themselves.

Yeah, in terms of a perfect design and where responsibility should lie what you've done feels right. However, because of the reality of our use cases (which I think would be 90%+ of the time we want the mount awareness) it feels like a lot of extra code.

useLayoutEffect() (and others) is a good example, but I think it's optimising for something we won't ever - or will rarely - use in reality.

Could we try the opt-out option? If it's not ideal, or presents issues, it can be reverted and we can merge this solution.

@afgomez
Copy link
Contributor Author

afgomez commented Oct 19, 2020

Could we try the opt-out option?

I'll give it a shot 👍

@afgomez
Copy link
Contributor Author

afgomez commented Oct 19, 2020

@elasticmachine merge upstream

The new flag will determine if the callbacks will trigger or the errors
will throw if the component using the hook is not mounted. By default
they will only trigger if it is.
@afgomez
Copy link
Contributor Author

afgomez commented Oct 27, 2020

@elasticmachine merge upstream

@afgomez afgomez requested a review from Kerry350 November 5, 2020 09:35
Copy link
Contributor

@Kerry350 Kerry350 left a comment

Choose a reason for hiding this comment

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

LGTM 🎉 Thanks for taking the time to switch around the solutions, and sorry for the delay in re-review.

@afgomez
Copy link
Contributor Author

afgomez commented Nov 9, 2020

@elasticmachine merge upstream

@afgomez
Copy link
Contributor Author

afgomez commented Nov 9, 2020

Thank you @Kerry350 🙇

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

async chunk count

id before after diff
infra 14 15 +1

async chunks size

id before after diff
infra 2.6MB 2.6MB -22.1KB

distributable file count

id before after diff
default 42763 42766 +3

page load bundle size

id before after diff
infra 174.0KB 174.2KB +151.0B

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@afgomez afgomez merged commit 6110ef8 into elastic:master Nov 9, 2020
@afgomez afgomez deleted the 69854-nagivate-away-errors branch November 9, 2020 11:43
gmmorris added a commit to gmmorris/kibana that referenced this pull request Nov 9, 2020
* master:
  Added `defaultActionMessage` to index threshold alert UI type definition (elastic#80936)
  [ILM] Migrate Delete phase and name field to Form Lib (elastic#82834)
  skip flaky suite (elastic#57426)
  [Alerting] adds an Run When field in the alert flyout to assign the action to an Action Group (elastic#82472)
  [APM] Expose APM event client as part of plugin contract (elastic#82724)
  [Logs UI] Fix errors during navigation (elastic#78319)
  Enable send to background in TSVB (elastic#82835)
  SavedObjects search_dsl: add match_phrase_prefix clauses when using prefix search (elastic#82693)
  [Ingest Manager] Unify install* under installPackage (elastic#82916)
afgomez pushed a commit that referenced this pull request Nov 9, 2020
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
phillipb added a commit to phillipb/kibana that referenced this pull request Nov 10, 2020
…e-details-overlay

* 'master' of github.com:elastic/kibana: (201 commits)
  Added `defaultActionMessage` to index threshold alert UI type definition (elastic#80936)
  [ILM] Migrate Delete phase and name field to Form Lib (elastic#82834)
  skip flaky suite (elastic#57426)
  [Alerting] adds an Run When field in the alert flyout to assign the action to an Action Group (elastic#82472)
  [APM] Expose APM event client as part of plugin contract (elastic#82724)
  [Logs UI] Fix errors during navigation (elastic#78319)
  Enable send to background in TSVB (elastic#82835)
  SavedObjects search_dsl: add match_phrase_prefix clauses when using prefix search (elastic#82693)
  [Ingest Manager] Unify install* under installPackage (elastic#82916)
  [Fleet] Make stream id unique in agent policy (elastic#82447)
  skip flaky suite (elastic#82915)
  skip flaky suite (elastic#75794)
  Copy `dateAsStringRt` to observability plugin (elastic#82839)
  [Maps] rename connected_components/map folder to mb_map (elastic#82897)
  [Security Solution] Fix EventsViewer DnD cypress tests (elastic#82619)
  [Security Solution] Adds logging and performance fan out API for threat/Indicator matching (elastic#82546)
  Implemented Alerting health status pusher by using task manager and status pooler for Kibana status plugins 'kibanahost/api/status' (elastic#79056)
  [APM] Adds new configuration 'xpack.apm.maxServiceEnvironments' (elastic#82090)
  Move single use function in line (elastic#82885)
  [ML] Add unsigned_long support to data frame analytics and anomaly detection (elastic#82636)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Fixes for quality problems that affect the customer experience Feature:Logs UI Logs UI feature release_note:skip Skip the PR/issue when compiling release notes Team:Infra Monitoring UI - DEPRECATED DEPRECATED - Label for the Infra Monitoring UI team. Use Team:obs-ux-infra_services v7.11.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Console warnings when navigating away from logs/metrics
6 participants