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

React.ReactNode allows passing {} as children which will crash at runtime #357

Closed
pomle opened this issue Oct 20, 2020 · 8 comments
Closed
Labels
BASIC Basic Cheatsheet good first issue Good for newcomers Hacktoberfest help wanted Extra attention is needed wontfix This will not be worked on

Comments

@pomle
Copy link

pomle commented Oct 20, 2020

In https://github.com/typescript-cheatsheets/react#useful-react-prop-type-examples it says

export declare interface AppProps {
  children: React.ReactNode; // best, accepts everything
}

ReactNode produces a false positive type check for {}.

React version: 16.14

Steps To Reproduce

  1. Create a new CRA project with TypeScript using for example yarn create react-app repro --template typescript
  2. Accept a prop using React.ReactNode as type in default App component.
import React from 'react';

type AppProps = {
  children: React.ReactNode;
}

function App({children}: AppProps) {
  return <div>{children}</div>;
}

export default App;
  1. Update index.tsx and supply {} as children to ```
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(
  <React.StrictMode>
    <App>{{}}</App>
  </React.StrictMode>,
  document.getElementById('root')
);

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();

The current behavior

Application crash at runtime with the error "Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead."

Screenshot 2020-10-20 at 19 17 56

The expected behavior

The error should have been detected at compile time.

@swyxio
Copy link
Collaborator

swyxio commented Oct 20, 2020

damn fantastic point. do you have an idea for alternative recommendation for typing children?

@swyxio swyxio added BASIC Basic Cheatsheet good first issue Good for newcomers Hacktoberfest help wanted Extra attention is needed labels Oct 20, 2020
@pomle
Copy link
Author

pomle commented Oct 21, 2020

@sw-yx, I am trying to figure out where this problem comes from still.

Correct me if I am wrong; this repo assumes types are derived from DefinitelyTyped?

Then, either the recommendation is wrong.

But it is possible that the type ReactNode from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L239 is wrong.

It inherits {} from type ReactFragment. I would not bet my life on it yet, but I think {} is way to wide for a ReactFragment.

Looking at the React source (typed in Flow afaict) it does not seem to agree
https://github.com/facebook/react/blob/master/packages/shared/ReactTypes.js#L20

Do you have any more insight, perhaps?

@swyxio
Copy link
Collaborator

swyxio commented Oct 21, 2020

well FYI the recommendation comes from me when I was learning R+TS so it's def not official. you're the first person in 2 years that has come along and raised any issue about it!

and as for the typing and whether they could be better, this is a @ferdaber and @eps1lon issue. I bet this has been raised before and we might learn something here about why React.Fragment has to accept the invalid {}.

@pomle
Copy link
Author

pomle commented Oct 21, 2020

Gotcha! Found the open issue at DefinitelyTyped/DefinitelyTyped#37596

Thanks for giving more context. I will keep this open until I understand more about the problem @DefinitelyTyped.

The next steps from me will be:

  • See if we can do something non-breaking to improve typing React nodes.
  • See if we can do a breaking change.
  • Recommend an alternate type from this repo and document the pitfalls.

@swyxio
Copy link
Collaborator

swyxio commented Oct 21, 2020

very reasonable. we can also leave a small warning to others in our notes, as a simple first step.

@pomle
Copy link
Author

pomle commented Oct 21, 2020

@sw-yx, absolutely. You know best what the readers will benefit from.

@stale
Copy link

stale bot commented Dec 20, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions!

@stale stale bot added the wontfix This will not be worked on label Dec 20, 2020
@swyxio swyxio closed this as completed in de3686e Dec 20, 2020
@swyxio
Copy link
Collaborator

swyxio commented Dec 20, 2020

added the warning as it is unlikely to be resolved for now

chef1119 pushed a commit to chef1119/chef-typescript that referenced this issue Oct 20, 2021
bernssolg added a commit to bernssolg/My-React-Sample that referenced this issue Feb 28, 2022
erinodev added a commit to erinodev/My-React-project that referenced this issue Feb 28, 2022
petardev101 added a commit to petardev101/react that referenced this issue Jun 4, 2022
supercrytoking added a commit to supercrytoking/react that referenced this issue Jul 14, 2022
kevindavies8 added a commit to kevindavies8/react-full-stack-developer that referenced this issue Aug 24, 2022
johnfrench3 pushed a commit to johnfrench3/react-Fronted-developer that referenced this issue Sep 7, 2022
ericbrown2716 added a commit to ericbrown2716/react-stack-build-website that referenced this issue Sep 29, 2022
peterjohnson4987 added a commit to peterjohnson4987/full-stack-developer-react that referenced this issue Oct 3, 2022
renawolford6 pushed a commit to renawolford6/react-husky-website that referenced this issue Oct 6, 2022
Yoshidayoshi23 added a commit to Yoshidayoshi23/react that referenced this issue Oct 20, 2022
renawolford6 added a commit to renawolford6/react-dev-build-doc- that referenced this issue Nov 10, 2022
coopfeathy added a commit to coopfeathy/cheatsheet that referenced this issue Dec 4, 2022
dreamcoder75 added a commit to dreamcoder75/react-sample that referenced this issue Jan 15, 2023
holyblock pushed a commit to holyblock/chart that referenced this issue Feb 27, 2023
AIDevMonster added a commit to AIDevMonster/Awesome-React that referenced this issue Jun 21, 2023
whiteghostDev added a commit to whiteghostDev/Awesome-React that referenced this issue Aug 6, 2023
cedev935 added a commit to cedev935/React-TypeScript that referenced this issue Sep 11, 2023
aleksandaralek added a commit to aleksandaralek/typescript-react-cheatsheet that referenced this issue Oct 24, 2023
xbucks pushed a commit to xbucks/react-cheatsheets that referenced this issue Oct 24, 2023
KonohaBrain125 pushed a commit to KonohaBrain125/React-Typescript that referenced this issue Oct 26, 2023
TOP-10-DEV added a commit to TOP-10-DEV/typescript-cheatsheets-react that referenced this issue Dec 8, 2023
secretsuperstar1109 added a commit to secretsuperstar1109/react-typescript-cheatsheets that referenced this issue Dec 9, 2023
champion119 added a commit to champion119/react that referenced this issue Jan 5, 2024
dragon360-dev added a commit to dragon360-dev/react that referenced this issue Mar 13, 2024
EugeneYoona added a commit to EugeneYoona/React_full_src that referenced this issue Apr 10, 2024
fairskyDev0201 added a commit to fairskyDev0201/typescript-cheatsheet that referenced this issue Apr 17, 2024
TechSolutionNinja added a commit to TechSolutionNinja/react that referenced this issue May 27, 2024
alisenola added a commit to alisenola/react-cheatsheets that referenced this issue May 29, 2024
LegendaryDev320 added a commit to LegendaryDev320/react that referenced this issue Jun 5, 2024
kaleb0402 added a commit to kaleb0402/react that referenced this issue Jul 18, 2024
Linda423 added a commit to Linda423/React that referenced this issue Jul 31, 2024
tosky19941209 added a commit to tosky19941209/React that referenced this issue Aug 7, 2024
touchsky941209 added a commit to touchsky941209/react-rebrand that referenced this issue Aug 26, 2024
genie4viz pushed a commit to genie4viz/react that referenced this issue Sep 2, 2024
hussammousa68 added a commit to hussammousa68/react that referenced this issue Oct 13, 2024
zeus-soft-world added a commit to zeus-soft-world/React-TypeScript that referenced this issue Oct 15, 2024
chivalrousdev added a commit to chivalrousdev/React that referenced this issue Nov 14, 2024
ElegantDev43 added a commit to ElegantDev43/react-test that referenced this issue Dec 15, 2024
firedev200043 added a commit to firedev200043/react-test that referenced this issue Dec 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BASIC Basic Cheatsheet good first issue Good for newcomers Hacktoberfest help wanted Extra attention is needed wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants