I'm trying to make a component that will show a loading circle when the prop isLoading is true, and otherwise show the child component. I'd like to use the component in other components likes this...
import Loading from './Loading.tsx'
...
const [isLoading,setLoading] = React.useState(false);
return (
<Loading isLoading={isLoading}>
<div>this component will show when loading turns to true</div>
</Loading> );
I'm getting the typscript error
Type '({ isLoading, color, children, }: PropsWithChildren<LoadingProps>) => Element | { children: ReactNode; }' is not assignable to type 'FunctionComponent<LoadingProps>'.
Type 'Element | { children: ReactNode; }' is not assignable to type 'ReactElement<any, any> | null'.
Type '{ children: ReactNode; }' is missing the following properties from type 'ReactElement<any, any>': type, props, key TS2322
Can someone point out what I'm doing wrong?
import React, { FunctionComponent } from 'react';
import { CircularProgress } from '@material-ui/core';
type LoadingProps = {
isLoading: boolean;
color: 'primary' | 'secondary' | 'inherit' | undefined;
};
const Loading: FunctionComponent<LoadingProps> = (props) => {
if(props.isLoading){
return <CircularProgress color={props.color || 'primary'} />
}
return props.children;
};
export default Loading;