Skip to content

Instantly share code, notes, and snippets.

@snamiki1212
Last active June 14, 2023 00:06
Show Gist options
  • Save snamiki1212/8b05e6e0a24d9ce9564dd840fe2d5742 to your computer and use it in GitHub Desktop.
Save snamiki1212/8b05e6e0a24d9ce9564dd840fe2d5742 to your computer and use it in GitHub Desktop.
/**
* 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