Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.memo with typescript in react

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???

like image 547
Varun Sharma Avatar asked Nov 22 '18 15:11

Varun Sharma


People also ask

Can you use TypeScript with React?

Create React App supports TypeScript out of the box. You can also add it to an existing Create React App project, as documented here.

Is React memo and useMemo same?

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.

Can we use React memo in functional component?

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.

Does React memo work with hooks?

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.


1 Answers

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

like image 184
sad comrade Avatar answered Sep 18 '22 18:09

sad comrade