Pass Children Prop To React Memo Component
I am currently in the process of optimising a number of React SFC components with the use of React.memo
.
Most of these components have children. The project also uses TypeScript.
I was starting to get the impression that memo components did not support children as I was presented with the following error message every time there was an error:
Property 'children' does not exist on type 'IntrinsicAttributes & object'
It turns out this only applies when I provide a Props type to the component. E.g.
const MyComponent: React.SFC<PropType>
If I remove the prop type (as below) then the error does not occur.
const MyComponent: React.SFC
Of course, I need the prop type in most every case.
I have setup a Code Sandbox that emulates the issue:
https://codesandbox.io/s/cool-keldysh-urddu?fontsize=14&hidenavigation=1&theme=dark
tl;dr: why do React.memo components not accept the child prop when a type has been provided for the component props?
Answer
This appears to be an issue with the React.memo
typings, it doesn't automatically pass through children props. There's a discussion on the DefinitelyTyped repo.
For now, you can manually specify the children
prop:
type ChildProps = {
name: string;
children: React.ReactNode;
};
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM