Skip to content

Commit 6424b9c

Browse files
Clarify that ReactNode edge case is only for React <18 (#504)
* Clarify that ReactNode edge case is only for React <18 * Update docs/basic/getting-started/basic-type-examples.md Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com> Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
1 parent 6592987 commit 6424b9c

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

README.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ const el2 = <MyArrayComponent />; // throws an error
403403
Unfortunately just annotating the function type will not help so if you really need to return other exotic types that React supports, you'd need to perform a type assertion:
404404

405405
```tsx
406-
const MyArrayComponent = () => Array(5).fill(<div />) as any as JSX.Element;
406+
const MyArrayComponent = () => (Array(5).fill(<div />) as any) as JSX.Element;
407407
```
408408

409409
[See commentary by @ferdaber here](https://github.com/typescript-cheatsheets/react-typescript-cheatsheet/issues/57).
@@ -1793,16 +1793,14 @@ This was done [on purpose](https://github.com/DefinitelyTyped/DefinitelyTyped/pu
17931793
```tsx
17941794
type Props = { children: React.ReactNode; type: "submit" | "button" };
17951795
export type Ref = HTMLButtonElement;
1796-
export const FancyButton = React.forwardRef(
1797-
(
1798-
props: Props,
1799-
ref: React.Ref<Ref> // <-- here!
1800-
) => (
1801-
<button ref={ref} className="MyClassName" type={props.type}>
1802-
{props.children}
1803-
</button>
1804-
)
1805-
);
1796+
export const FancyButton = React.forwardRef((
1797+
props: Props,
1798+
ref: React.Ref<Ref> // <-- here!
1799+
) => (
1800+
<button ref={ref} className="MyClassName" type={props.type}>
1801+
{props.children}
1802+
</button>
1803+
));
18061804
```
18071805

18081806
</details>

docs/basic/getting-started/basic-type-examples.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ export declare interface AppProps {
7474
```
7575

7676
<details>
77-
<summary><b>Small <code>React.ReactNode</code> edge case</b></summary>
77+
<summary><b>Small <code>React.ReactNode</code> edge case before React 18</b></summary>
7878

79-
This code typechecks but has a runtime error:
79+
Before the [React 18 type updates](https://github.com/DefinitelyTyped/DefinitelyTyped/pull/56210), this code typechecked but had a runtime error:
8080

8181
```tsx
8282
type Props = {
@@ -87,16 +87,16 @@ function Comp({ children }: Props) {
8787
return <div>{children}</div>;
8888
}
8989
function App() {
90-
return <Comp>{{}}</Comp>; // Runtime Error: Objects not valid as React Child!
90+
// Before React 18: Runtime error "Objects are not valid as a React child"
91+
// After React 18: Typecheck error "Type '{}' is not assignable to type 'ReactNode'"
92+
return <Comp>{{}}</Comp>;
9193
}
9294
```
9395

94-
This is because `ReactNode` includes `ReactFragment` which allows a `{}` type, which is [too wide](https://github.com/DefinitelyTyped/DefinitelyTyped/issues/37596#issue-480260937). Fixing this would break a lot of libraries, so for now you just have to be mindful that `ReactNode` is not absolutely bulletproof.
96+
This is because `ReactNode` includes `ReactFragment` which allowed type `{}` before React 18.
9597

9698
[Thanks @pomle for raising this.](https://github.com/typescript-cheatsheets/react/issues/357)
9799

98-
With the [React 18 type updates](https://github.com/DefinitelyTyped/DefinitelyTyped/pull/56210), `{}` is no longer allowed in `ReactFragment`.
99-
100100
</details>
101101

102102
<details>

0 commit comments

Comments
 (0)