kibana/packages/shared-ux/router/impl
Anton Dosov fccfd4cf75
[react@18] Implicit children type fixes (#192011)
## Summary

Part of https://github.com/elastic/kibana/issues/138222

in @types/react@18 types [have become more
strict](https://github.com/DefinitelyTyped/DefinitelyTyped/pull/56210).
This PR addresses a bunch of easy fixes. The most common are:

### 1 Removal of implicit children

For components that do implement children but relied on their implicit
declaration from React.FunctionComponent or React.Component:

```diff
 interface Props {
+  children?: React.ReactNode;
 }

 class SomeClassComponents React.Component<Props> {
   render() {
     return  <div>{this.props.children}</div>
   }
 }
 const SomeFunctionComponent: React.FunctionComponent<Props> = props => <div>{props.children}</div>
```

or 

```diff
- const SomeFunctionComponent: React.FunctionComponent<Props> = props => <div>{props.children}</div>
+ const SomeFunctionComponent: React.FunctionComponent<React.PropsWithChildren<Props>> = props => <div>{props.children}</div>
```


*Note 1:*
The most common change occurs in unit tests where `renderHook` and
`wrapper` are used. I had to re-type the props so that `children` were
there

```diff
const { result } = renderHook(
         () => {
           return useLicense();
         },
         {
-           wrapper: ({ children }) => (
+           wrapper: ({ children }: React.PropsWithChildren<{}>) => (
             <TestProviders license={license}>{children}</TestProviders>
           ),
         }
       );
```

```diff
- const { result } = renderHook<GetCasesColumn, UseCasesColumnsReturnValue>(
+ const { result } = renderHook<React.PropsWithChildren<GetCasesColumn>, UseCasesColumnsReturnValue>(
       () => useCasesColumns(defaultColumnArgs),
       {
         wrapper: ({ children }) => <TestProviders>{children}</TestProviders>,
       }
     );
```



*Note 2:*
With @types/react@17 the components that don't have any props apart from
`children` I had to use `React.PropsWithChildren<{}>`, whereas in
@types/react@18 the argument becomes optional, so it can be omitted, and
type simpler with `React.PropsWithChildren` without an argument



### 2 `this.context` becomes `unknown` (was `any`)

In a couple of places where `this.context` is used, I had to type it

### 3 `ComponentType` from enzyme is no longer compatible with
`ComponentType` from react

This one is a bummer, but where `wrappingComponent` with enzyme is used
I had to cast it to type from `enzyme`

```diff
- import { mount } from 'enzyme'
+ import { mount, ComponentType } from 'enzyme'

 wrapper = mount(<ClosureOptions {...props} />, {
-       wrappingComponent: TestProviders,
+       wrappingComponent: TestProviders as ComponentType<{}>,
});
```
2024-09-09 13:56:02 +02:00
..
__snapshots__ [shared-ux-router] Add Router and Routes components (#159834) 2023-06-23 10:02:06 -05:00
index.ts Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
jest.config.js Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kibana.jsonc [codeowners] rename global experience to @elastic/appex-sharedux 2023-01-18 10:02:49 -07:00
package.json Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
README.mdx
route.test.tsx Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
route.tsx Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
router.tsx Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
routes.tsx [react@18] Implicit children type fixes (#192011) 2024-09-09 13:56:02 +02:00
services.ts Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
tsconfig.json Transpile packages on demand, validate all TS projects (#146212) 2022-12-22 19:00:29 -06:00
types.ts Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
use_execution_context.ts Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00

---
id: sharedUX/Router
slug: /shared-ux/router
title: Router
description: A router component
tags: ['shared-ux', 'component', 'router', 'route']
date: 2022-08-12
---

## Summary
This is a wrapper around the `react-router-dom` Route component that inserts MatchPropagator in every application route. It helps track all route changes and send them to the execution context, later used to enrich APM 'route-change' transactions.
The component does not require any props and accepts props from the RouteProps interface such as a `path`, or a component like `AppContainer`. 


```jsx 
<Route path="/url" />
```

### Explanation of RouteProps

```jsx
export interface RouteProps {
    location?: H.Location; 
    component?: React.ComponentType<RouteComponentProps<any>> | React.ComponentType<any>; 
    render?: (props: RouteComponentProps<any>) => React.ReactNode; 
    children?: ((props: RouteChildrenProps<any>) => React.ReactNode) | React.ReactNode; 
    path?: string | string[]; 
    exact?: boolean; 
    sensitive?: boolean; 
    strict?: boolean; 
}
```

All props are optional 

| Prop Name | Prop Type | Description |
|---|---|---|
| `location` | `H.Location` | the location of one instance of history |
| `component` | `React.ComponentType<RouteComponentProps<any>>` or `React.ComponentType<any>;` | a react component |
| `render` | `(props: RouteComponentProps<any>) => React.ReactNode;` | render props to a react node|
| `children` | `((props: RouteChildrenProps<any>) => React.ReactNode)` or `React.ReactNode;` | pass children to a react node |
| `path` | `string` or `string[];` | a url path or array of paths |
| `exact` | `boolean` | exact match for a route (see: https://stackoverflow.com/questions/52275146/usage-of-exact-and-strict-props) |
| `sensitive` | `boolean` | case senstive route |
| `strict` | `boolean` | strict entry of the requested path in the path name |



This component removes the need for manual calls to `useExecutionContext` and they should be removed. 

## EUI Promotion Status

This component is not currently considered for promotion to EUI.