I am using React.memo() in a .tsx file(React typescript)
Now I have declared an interface for Props as:
interface Props {
type?: string;
}
My component looks like:
const Component: React.SFC<Props> = props => {
/// return something;
};
export default memo(Component);
Now since type is an optional prop I intend to use it only sometimes.
If I use my component as <Component type="something" />
everything is okay.
But if I use it as <Component />
I get the error -->
Type '{ children: string; }' has no properties in common with type 'IntrinsicAttributes & Props'.
This seems to be a very absurd error as I have tried this on Code sandbox and everything works fine. Any ideas about what the error could be?
UPDATE
If I explicitly add a prop in interface like
interface Props {
type?: string;
children?: ReactNode;
}
then in that case everything works fine. This is supposed to be implicit but acc to the error its taking as
'{ children: string; }'
Any ideas???
Create React App supports TypeScript out of the box. You can also add it to an existing Create React App project, as documented here.
memo is for when you find an existing component that is expensive to render, and you don't have the option to optimize it internally. Just wrap it and let React do an extra check. React. useMemo is for internally optimizing a component by saving the return value of an expensive function call.
Don't use React Memo if the component isn't heavy and renders with different props altogether. Wrapping a class-based component in React Memo is undesirable and therefore should not be used. Instead, you can extend PureComponent or implement shouldComponentUpdate() method.
memo() and hooks. Components using hooks can be freely wrapped in React. memo() to achieve memoization. React always re-renders the component if the state changes, even if the component is wrapped in React.
I believe there is an error in typings. Changing
function memo<P extends object>(
Component: SFC<P>,
propsAreEqual?: (
prevProps: Readonly<PropsWithChildren<P>>,
nextProps: Readonly<PropsWithChildren<P>>
) => boolean
): NamedExoticComponent<P>;
to
function memo<P extends object>(
Component: SFC<P>,
propsAreEqual?: (
prevProps: Readonly<PropsWithChildren<P>>,
nextProps: Readonly<PropsWithChildren<P>>
) => boolean
): NamedExoticComponent<PropsWithChildren<P>>;
fixes the issue
UPDATE
Well, actually it is not a bug. See discussion.
TLTR: You have to specify children
in component props type.
type MyComponentProps {
children: React.ReactNode;
}
Read more
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With