Last active
June 14, 2023 00:06
-
-
Save snamiki1212/8b05e6e0a24d9ce9564dd840fe2d5742 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Check to render null/undefined/component. | |
* | |
* Conclusion: `{makeComponent(...)}` is better than `<MakeComponent ... />` because of `undefined` case. | |
*/ | |
const Component = () => { | |
const makeNull = () => null; | |
const makeUndefined = () => undefined; | |
const makeComponent = (props: { item: string }) => ( | |
<div>makeComponent: {props.item}</div> | |
); | |
const ImplNull = () => null; | |
const ImplUndefined = () => undefined; | |
const ImplComponent = (props: { item: string }) => ( | |
<div>ImplComponent: {props.item}</div> | |
); | |
return ( | |
<div className="App"> | |
{makeNull()} // ok: no render | |
{makeUndefined()} // ok: no render | |
{makeComponent({ item: "ok" })} // ok: render | |
<ImplNull /> // ok: no nreder | |
<ImplUndefined />. // ng: ts error 'ImplUndefined' cannot be used as a JSX component. Its return type 'undefined' is not a valid JSX element.ts | |
<ImplComponent item="ok" />. // ok: render | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment