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

Update react-router-dom to v5 #46885

Closed
wants to merge 3 commits into from

Conversation

jfsiii
Copy link
Contributor

@jfsiii jfsiii commented Sep 29, 2019

Summary

Update react-router-dom & @types/react-router-dom to 5.x branch (5.1.1).

According to the announcement for 5.0:

There are no breaking changes in this release. If you are already using version 4.x, you can use version 5 immediately with zero code changes. πŸŽ‰ Version 5 will pick up where the 4.x roadmap (now the 5.x roadmap) left off.

5.0 is also removes all warnings for React.StrictMode

5.1 adds some hooks (useParams, useLocation, useHistory, etc) See the announcement for more details.

Checklist

Use strikethroughs to remove checklist items you don't feel are applicable to this PR.

For maintainers

Details about upgrading

I had to manually yarn.lock due to this issue When I first ran `kbn bootstrap` it failed with
[bootstrap] failed:

Error: Command failed: yarn run kbn:bootstrap
error Command failed with exit code 2.
error Command failed with exit code 2.

$ yarn build:types && node scripts/register_git_hook
$ tsc --p tsconfig.types.json
node_modules/@types/react-router-dom/index.d.ts(32,5): error TS2305: Module '"../../../../../../../Users/jfsiii/work/kibana/node_modules/@types/react-router-dom/node_modules/@types/react-router"' has no exported member 'useHistory'.
node_modules/@types/react-router-dom/index.d.ts(33,5): error TS2305: Module '"../../../../../../../Users/jfsiii/work/kibana/node_modules/@types/react-router-dom/node_modules/@types/react-router"' has no exported member 'useLocation'.
node_modules/@types/react-router-dom/index.d.ts(34,5): error TS2305: Module '"../../../../../../../Users/jfsiii/work/kibana/node_modules/@types/react-router-dom/node_modules/@types/react-router"' has no exported member 'useParams'.
node_modules/@types/react-router-dom/index.d.ts(35,5): error TS2305: Module '"../../../../../../../Users/jfsiii/work/kibana/node_modules/@types/react-router-dom/node_modules/@types/react-router"' has no exported member 'useRouteMatch'.

I removed the "@types/react-router@*": block and re-ran `yarn add react-router-dom@^5.1.0 && yarn add -D @types/react-router-dom@^5.1.0

I added path-to-regexp@^1.7.0 as an explicit dependency due to this issue `yarn start` initially errored with
server    log   [12:10:30.797] [warning][plugin] ENOENT: Unable to scan directory for plugins "/Users/jfsiii/work/kibana/plugins"
optmzr    log   [12:10:33.854] [fatal][root] TypeError: _pathToRegexp.default.compile is not a function
    at Object.compile (/Users/jfsiii/work/kibana/x-pack/legacy/plugins/code/common/uri_util.ts:117:31)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Module._compile (/Users/jfsiii/work/kibana/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Object.newLoader [as .ts] (/Users/jfsiii/work/kibana/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/Users/jfsiii/work/kibana/x-pack/legacy/plugins/code/common/repository_utils.ts:13:1)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Module._compile (/Users/jfsiii/work/kibana/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Object.newLoader [as .ts] (/Users/jfsiii/work/kibana/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:599:32)

 FATAL  TypeError: _pathToRegexp.default.compile is not a function

I tried the import * as pathToRegexp from 'path-to-regexp' change, but that just resulted in FATAL TypeError: pathToRegexp is not a function

Then I noticed yarn list --pattern path-to-regexp showed 0.1.7 at the root,

β”œβ”€ fetch-mock@7.3.9
β”‚  └─ path-to-regexp@2.4.0
β”œβ”€ nise@1.5.2
β”‚  └─ path-to-regexp@1.7.0
β”œβ”€ path-to-regexp@0.1.7
└─ react-router@5.1.1
   └─ path-to-regexp@1.7.0

so I ran yarn add path-to-regexp@^1.7.0 since that's the version react-router uses and the errors went away

@jfsiii jfsiii requested a review from spalger September 29, 2019 16:54
@jfsiii
Copy link
Contributor Author

jfsiii commented Sep 29, 2019

@spalger I wasn't sure if I should tag teams which use react-router-dom (infra, code, amp, siem, etc).

@elasticmachine
Copy link
Contributor

πŸ’” Build Failed

@elasticmachine
Copy link
Contributor

πŸ’” Build Failed

@jfsiii
Copy link
Contributor Author

jfsiii commented Sep 30, 2019

I'm not sure how to deal with these CI failures. Here are some notes on each.

I've cc'd some people from the sections which are failing as a way to gain insight into how/why they're failing. This PR is supposed to be non-breaking so I'm looking for some clues into what might have gone wrong (e.g. updated a related lib to a new version?). Please don't think you need to fix the issues here. I'd just appreciate any thoughts you have on what might be going on. I'd love to get us to React Router 5 and would appreciate any help you can give; whatever that looks like.

Type Check

Errors seem related to these tickets about withRouter DefinitelyTyped/DefinitelyTyped#38271, DefinitelyTyped/DefinitelyTyped#38326, microsoft/TypeScript#33490 which seem in flux

I got rid of the errors using this diff, but I'm not sure if it's correct

diff to fix type check errors https://github.com/microsoft/TypeScript/issues/33490
diff --git a/x-pack/legacy/plugins/beats_management/public/components/layouts/no_data.tsx b/x-pack/legacy/plugins/beats_management/public/components/layouts/no_data.tsx
index 8d4c7703df..e266c47a60 100644
--- a/x-pack/legacy/plugins/beats_management/public/components/layouts/no_data.tsx
+++ b/x-pack/legacy/plugins/beats_management/public/components/layouts/no_data.tsx
@@ -14,8 +14,8 @@ interface LayoutProps {
   modalClosePath?: string;
 }
 
-export const NoDataLayout: React.SFC<LayoutProps> = withRouter<any>(
-  ({ actionSection, title, modalClosePath, children, history }) => {
+export const NoDataLayout = withRouter<any, React.FunctionComponent<LayoutProps>>(
+  ({ actionSection, title, modalClosePath, children }) => {
     return (
       <EuiFlexGroup justifyContent="spaceAround">
         <EuiFlexItem grow={false}>
@@ -31,4 +31,4 @@ export const NoDataLayout: React.SFC<LayoutProps> = withRouter<any>(
       </EuiFlexGroup>
     );
   }
-) as any;
+);
diff --git a/x-pack/legacy/plugins/beats_management/public/components/navigation/connected_link.tsx b/x-pack/legacy/plugins/beats_management/public/components/navigation/connected_link.tsx
index 30d12c9ce1..44c6822747 100644
--- a/x-pack/legacy/plugins/beats_management/public/components/navigation/connected_link.tsx
+++ b/x-pack/legacy/plugins/beats_management/public/components/navigation/connected_link.tsx
@@ -8,6 +8,14 @@ import React from 'react';
 import { EuiLink } from '@elastic/eui';
 import { Link, withRouter } from 'react-router-dom';
 
+interface ConnectedLinkComponentProps {
+  location: any;
+  path: string;
+  disabled: boolean;
+  query: any;
+  [key: string]: any;
+}
+
 export function ConnectedLinkComponent({
   location,
   path,
@@ -15,13 +23,7 @@ export function ConnectedLinkComponent({
   disabled,
   children,
   ...props
-}: {
-  location: any;
-  path: string;
-  disabled: boolean;
-  query: any;
-  [key: string]: any;
-}) {
+}: ConnectedLinkComponentProps) {
   if (disabled) {
     return <EuiLink aria-disabled="true" {...props} />;
   }
@@ -38,4 +40,6 @@ export function ConnectedLinkComponent({
   );
 }
 
-export const ConnectedLink = withRouter<any>(ConnectedLinkComponent);
+export const ConnectedLink = withRouter<any, React.FC<ConnectedLinkComponentProps>>(
+  ConnectedLinkComponent
+);
diff --git a/x-pack/legacy/plugins/beats_management/public/containers/with_url_state.tsx b/x-pack/legacy/plugins/beats_management/public/containers/with_url_state.tsx
index 0802b4d8ea..ad5fd767c0 100644
--- a/x-pack/legacy/plugins/beats_management/public/containers/with_url_state.tsx
+++ b/x-pack/legacy/plugins/beats_management/public/containers/with_url_state.tsx
@@ -86,7 +86,7 @@ export class WithURLStateComponent<URLState extends object> extends React.Compon
     });
   };
 }
-export const WithURLState = withRouter<any>(WithURLStateComponent);
+export const WithURLState = withRouter<any, any>(WithURLStateComponent);
 
 export function withUrlState<OP>(
   UnwrappedComponent: React.ComponentType<OP & URLStateProps>

/cc @mattapperson

CI Group 1,4

The failures for Group 1 and Group 4 are allCannot read property 'history' of undefined which I believe is trying to be read from a history object coming from import, this or props.

This is supposed to be a non-breaking change, so these make me uncomfortable. However, the Group 4 failures seem confined to routing.getRouterLinkProps called from FollowerIndicesList, so perhaps it's an actual bug / missed router assignment? Still ...

/cc @cjcenizal @sebelga since I saw your names in the history when looking at these lines

CI Group 2

The Group 2 failures have some "uncaught at handleDocumentSearch" "TypeError: Cannot read property 'map' of undefined which appear to come from a redux-saga

/cc @mw-ding b/c I saw your name in the tests

@jfsiii
Copy link
Contributor Author

jfsiii commented Sep 30, 2019

@elasticmachine merge upstream

@mattapperson
Copy link
Contributor

@jfsiii Sorry, can you link to the CI failure you are seeing for types? I can't seem to find it in the CI logs of the last fail

@jfsiii
Copy link
Contributor Author

jfsiii commented Sep 30, 2019

@mattapperson This is from the first failed run, but the errors are the same https://kibana-ci.elastic.co/job/elastic+kibana+pipeline-pull-request/1443/execution/node/45/log/
update: this check failed right after I commented https://github.com/elastic/kibana/pull/46885/checks?check_run_id=242045083

I accidentally committed the diff I posted so the last run passed without type errors, but I've since reverted that and expect the third run to fail like the first.

Like I said, I think the issue is with the @types defintions or maybe TS itself (see the links). I wanted to silence them using the new/current signature, but maybe @ts-ignore is another option.

@cjcenizal
Copy link
Contributor

@sebelga In #36871 you mentioned this:

When running the tests we get the warning Warning: componentWillMount is deprecated and will be removed in the next major version (...) Please update the following components: MemoryRouter, Route, Router, Switch. Updating to react-router v5.0.0 (which is backward compatible) removes the warning.... but break 3 of our apps (remote clusters, ccr and rollups) as they depend on the non-official old "Context" to access the router. The fix should not be too complex but maybe other plugins have the same issue and we probably want to make sure all our plugins are compatible with react-router v5 and allow ourselves updating to react 17.0.0

Were the things that broke the same things that @jfsiii is reporting? If so, then it sounds like we just need to migrate these three apps off of the context-based method of accessing the router. If this is the case, could you provide @jfsiii with steps for doing that, so he can make those changes in this PR?

@elasticmachine
Copy link
Contributor

πŸ’” Build Failed

@mattapperson
Copy link
Contributor

@jfsiii I think the types changed in ReactRouter from the docs it looks like it needs to be adjusted to:

interface ComponentProps extends RouteComponentProps {
title: string | React.ReactNode;
actionSection?: React.ReactNode;
modalClosePath?: string;
}

and then type the arguments as it is no longer inferred in this package through to the component

@elasticmachine
Copy link
Contributor

πŸ’” Build Failed

@jfsiii
Copy link
Contributor Author

jfsiii commented Sep 30, 2019

@mattapperson Not sure if this is what you were thinking but it also clears the type warnings:

diff --git a/x-pack/legacy/plugins/beats_management/public/components/layouts/no_data.tsx b/x-pack/legacy/plugins/beats_management/public/components/layouts/no_data.tsx
index 8d4c7703df..e316f6d146 100644
--- a/x-pack/legacy/plugins/beats_management/public/components/layouts/no_data.tsx
+++ b/x-pack/legacy/plugins/beats_management/public/components/layouts/no_data.tsx
@@ -5,17 +5,17 @@
  */
 
 import { EuiEmptyPrompt, EuiFlexGroup, EuiFlexItem, EuiPageContent } from '@elastic/eui';
-import React from 'react';
-import { withRouter } from 'react-router-dom';
+import React, { ComponentProps } from 'react';
+import { withRouter, RouteComponentProps } from 'react-router-dom';
 
-interface LayoutProps {
+interface LayoutProps extends ComponentProps<any>, RouteComponentProps {
   title: string | React.ReactNode;
   actionSection?: React.ReactNode;
   modalClosePath?: string;
 }
 
-export const NoDataLayout: React.SFC<LayoutProps> = withRouter<any>(
-  ({ actionSection, title, modalClosePath, children, history }) => {
+export const NoDataLayout = withRouter(
+  ({ actionSection, title, modalClosePath, children, history }: LayoutProps) => {
     return (
       <EuiFlexGroup justifyContent="spaceAround">
         <EuiFlexItem grow={false}>
@@ -31,4 +31,4 @@ export const NoDataLayout: React.SFC<LayoutProps> = withRouter<any>(
       </EuiFlexGroup>
     );
   }
-) as any;
+);
diff --git a/x-pack/legacy/plugins/beats_management/public/components/navigation/connected_link.tsx b/x-pack/legacy/plugins/beats_management/public/components/navigation/connected_link.tsx
index 30d12c9ce1..501ddaf410 100644
--- a/x-pack/legacy/plugins/beats_management/public/components/navigation/connected_link.tsx
+++ b/x-pack/legacy/plugins/beats_management/public/components/navigation/connected_link.tsx
@@ -3,10 +3,18 @@
  * or more contributor license agreements. Licensed under the Elastic License;
  * you may not use this file except in compliance with the Elastic License.
  */
-import React from 'react';
+import React, { ComponentProps } from 'react';
 
 import { EuiLink } from '@elastic/eui';
-import { Link, withRouter } from 'react-router-dom';
+import { Link, RouteComponentProps, withRouter } from 'react-router-dom';
+
+interface ConnectedLinkComponentProps extends ComponentProps<any>, RouteComponentProps {
+  location: any;
+  path: string;
+  disabled: boolean;
+  query: any;
+  [key: string]: any;
+}
 
 export function ConnectedLinkComponent({
   location,
@@ -15,13 +23,7 @@ export function ConnectedLinkComponent({
   disabled,
   children,
   ...props
-}: {
-  location: any;
-  path: string;
-  disabled: boolean;
-  query: any;
-  [key: string]: any;
-}) {
+}: ConnectedLinkComponentProps) {
   if (disabled) {
     return <EuiLink aria-disabled="true" {...props} />;
   }
@@ -38,4 +40,4 @@ export function ConnectedLinkComponent({
   );
 }
 
-export const ConnectedLink = withRouter<any>(ConnectedLinkComponent);
+export const ConnectedLink = withRouter(ConnectedLinkComponent);
diff --git a/x-pack/legacy/plugins/beats_management/public/containers/with_url_state.tsx b/x-pack/legacy/plugins/beats_management/public/containers/with_url_state.tsx
index 0802b4d8ea..92dead2b70 100644
--- a/x-pack/legacy/plugins/beats_management/public/containers/with_url_state.tsx
+++ b/x-pack/legacy/plugins/beats_management/public/containers/with_url_state.tsx
@@ -6,7 +6,7 @@
 
 import { parse, stringify } from 'querystring';
 import React from 'react';
-import { withRouter } from 'react-router-dom';
+import { RouteComponentProps, withRouter } from 'react-router-dom';
 import { FlatObject } from '../frontend_types';
 import { RendererFunction } from '../utils/typed_react';
 
@@ -22,7 +22,7 @@ export interface URLStateProps<URLState = object> {
   ) => void;
   urlState: URLState;
 }
-interface ComponentProps<URLState extends object> {
+interface ComponentProps<URLState extends object> extends RouteComponentProps {
   history: any;
   match: any;
   children: RendererFunction<URLStateProps<URLState>>;
@@ -86,7 +86,7 @@ export class WithURLStateComponent<URLState extends object> extends React.Compon
     });
   };
 }
-export const WithURLState = withRouter<any>(WithURLStateComponent);
+export const WithURLState = withRouter(WithURLStateComponent);
 
 export function withUrlState<OP>(
   UnwrappedComponent: React.ComponentType<OP & URLStateProps>

@mattapperson
Copy link
Contributor

Yup, that's exactly what I was attempting to recommend @jfsiii ! Glad you got it to work 😊

Copy link
Contributor

@spalger spalger left a comment

Choose a reason for hiding this comment

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

kbn-ui-framework looks good, except for the notes I left. LGTM after those are fixed

package.json Outdated
@@ -205,6 +205,7 @@
"node-fetch": "1.7.3",
"opn": "^5.5.0",
"oppsy": "^2.0.0",
"path-to-regexp": "^1.7.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

We depend on this in the code project, but it's not listed in the x-pack package.json, maybe we can list it there?

@@ -22,6 +22,7 @@
"react": "^16.2.0",
"react-ace": "^5.9.0",
"react-color": "^2.13.8",
"react-router-dom": "^5.1.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

this is a dev dependency for the docs site and is listed down there.

@@ -33,6 +34,7 @@
"@babel/core": "^7.5.5",
"@elastic/eui": "0.0.55",
"@kbn/babel-preset": "1.0.0",
"@types/react-router-dom": "^5.1.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

This package doesn't use TS, so the types aren't necessary.

@sebelga
Copy link
Contributor

sebelga commented Oct 1, 2019

@cjcenizal yes the error Cannot read property 'history' of undefined is related to accessing the router v4 context to keep a reference to the router.

The workaround is the one I wrote in eui: https://github.com/elastic/eui/blob/master/wiki/react-router.md#react-router-5x

We can discuss later at our sync if we have time to upgrade our apps before ff.

cc @jfsiii

@jfsiii jfsiii force-pushed the update-react-router-dom-to-v5 branch from e95f1a4 to 87f087e Compare October 1, 2019 14:41
@jfsiii
Copy link
Contributor Author

jfsiii commented Oct 1, 2019

@elasticmachine merge upstream

@elasticmachine
Copy link
Contributor

πŸ’” Build Failed

@jfsiii
Copy link
Contributor Author

jfsiii commented Oct 30, 2019

@elasticmachine merge upstream

@elasticmachine
Copy link
Contributor

merge conflict between base and head

@jfsiii jfsiii closed this Oct 30, 2019
@jfsiii jfsiii force-pushed the update-react-router-dom-to-v5 branch from db3fb4b to 9f7bc64 Compare October 30, 2019 17:24
@elasticmachine
Copy link
Contributor

πŸ’” Build Failed

John Schulz added 2 commits October 30, 2019 17:21
Initially got the errors described in DefinitelyTyped/DefinitelyTyped#38578 (comment) but I followed DefinitelyTyped/DefinitelyTyped#38578 (comment) and deleted the `@types/react-router` entry in `yarn.lock`
Issues are described in DefinitelyTyped/DefinitelyTyped#38271

Root cause is stated to be an issue with TS microsoft/TypeScript#34607 which has since been fixed in master.

Commiting now to prevent issues, but would also like to revert this and try again when TS fix ships (TS 3.7?)
@jfsiii jfsiii reopened this Oct 30, 2019
@elasticmachine
Copy link
Contributor

πŸ’” Build Failed

IIRC, I added this while tracking down the issues described in DefinitelyTyped/DefinitelyTyped#38271

It could have been another error, but we're not _supposed_ to have to add this dependency. I added it trying to suppress errors. Seeing if I can remove it and have the same results we currently have.
@elasticmachine
Copy link
Contributor

πŸ’” Build Failed

@jfsiii
Copy link
Contributor Author

jfsiii commented Dec 11, 2019

Closing thanks to #52445

@jfsiii jfsiii closed this Dec 11, 2019
@jfsiii jfsiii deleted the update-react-router-dom-to-v5 branch December 30, 2019 16:33
@jen-huang jen-huang added the Team:Fleet Team label for Observability Data Collection Fleet team label Mar 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team:Fleet Team label for Observability Data Collection Fleet team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants