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

Allow children to be a ReactNode instead of ReactElement #88

Merged
merged 3 commits into from May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -27,7 +27,7 @@ npm install --save react-placeholder
### Props

```tsx
children: ReactElement | null;
children: ReactNode;
ready: boolean;
delay?: number;
firstLaunchOnly?: boolean;
Expand Down
11 changes: 9 additions & 2 deletions src/ReactPlaceholder.tsx
Expand Up @@ -3,7 +3,7 @@ import * as placeholders from './placeholders';
import { joinClassNames } from './utils';

type CommonProps = {
children: React.ReactElement | null;
children: React.ReactNode;
/** pass `true` when the content is ready and `false` when it's loading */
ready: boolean;
/** delay in millis to wait when passing from ready to NOT ready */
Expand Down Expand Up @@ -147,7 +147,14 @@ const ReactPlaceholder: React.FC<Props> = ({
[]
);

return ready ? children : getFiller();
// Casting - workaround for DefinitelyTyped/react issue with
// FunctionComponents returning ReactElement, where they should be able to
// return ReactNode. Casting also doesn't introduce another layer in the
// component tree like another `<>children</>` workaround does.
//
// See https://github.com/DefinitelyTyped/DefinitelyTyped/issues/33006
// and https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18051
return ready ? children as React.ReactElement : getFiller();
};

export default ReactPlaceholder;