Given a simple component that renders its children:
class ContainerComponent extends Component {
static propTypes = {
children: PropTypes.object.isRequired,
}
render() {
return (
<div>
{this.props.children}
</div>
);
}
}
export default ContainerComponent;
Question: What should the propType of the children prop be?
When I set it as an object, it fails when I use the component with multiple children:
<ContainerComponent>
<div>1</div>
<div>2</div>
</ContainerComponent>
Warning: Failed prop type: Invalid prop
children
of typearray
supplied toContainerComponent
, expectedobject
.
If I set it as an array, it will fail if I give it only one child, i.e.:
<ContainerComponent>
<div>1</div>
</ContainerComponent>
Warning: Failed prop type: Invalid prop children of type object supplied to ContainerComponent, expected array.
Please advise, should I just not bother doing a propTypes check for children elements?
This question is related to
reactjs
jsx
react-proptypes
For me it depends on the component. If you know what you need it to be populated with then you should try to specify exclusively, or multiple types using:
PropTypes.oneOfType
If you want to refer to a React component then you will be looking for
PropTypes.element
Although,
PropTypes.node
describes anything that can be rendered - strings, numbers, elements or an array of these things. If this suits you then this is the way.
With very generic components, who can have many types of children, you can also use the below - though bare in mind that eslint and ts may not be happy with this lack of specificity:
PropTypes.any
Example:
import React from 'react';
import PropTypes from 'prop-types';
class MenuItem extends React.Component {
render() {
return (
<li>
<a href={this.props.href}>{this.props.children}</a>
</li>
);
}
}
MenuItem.defaultProps = {
href: "/",
children: "Main page"
};
MenuItem.propTypes = {
href: PropTypes.string.isRequired,
children: PropTypes.string.isRequired
};
export default MenuItem;
Picture: Shows you error in console if the expected type is different
If you want to include render prop components:
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
PropTypes.func
])
The PropTypes documentation has the following
// Anything that can be rendered: numbers, strings, elements or an array
// (or fragment) containing these types.
optionalNode: PropTypes.node,
So, you can use PropTypes.node
to check for objects or arrays of objects
static propTypes = {
children: PropTypes.node.isRequired,
}
If you want to match exactly a component type, check this
MenuPrimary.propTypes = {
children: PropTypes.oneOfType([
PropTypes.arrayOf(MenuPrimaryItem),
PropTypes.objectOf(MenuPrimaryItem)
])
}
If you want to match exactly some component types, check this
const HeaderTypes = [
PropTypes.objectOf(MenuPrimary),
PropTypes.objectOf(UserInfo)
]
Header.propTypes = {
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([...HeaderTypes])),
...HeaderTypes
])
}
Try a custom propTypes :
const childrenPropTypeLogic = (props, propName, componentName) => {
const prop = props[propName];
return React.Children
.toArray(prop)
.find(child => child.type !== 'div') && new Error(`${componentName} only accepts "div" elements`);
};
static propTypes = {
children : childrenPropTypeLogic
}
const {Component, PropTypes} = React;_x000D_
_x000D_
const childrenPropTypeLogic = (props, propName, componentName) => {_x000D_
var error;_x000D_
var prop = props[propName];_x000D_
_x000D_
React.Children.forEach(prop, function (child) {_x000D_
if (child.type !== 'div') {_x000D_
error = new Error(_x000D_
'`' + componentName + '` only accepts children of type `div`.'_x000D_
);_x000D_
}_x000D_
});_x000D_
_x000D_
return error;_x000D_
};_x000D_
_x000D_
_x000D_
_x000D_
class ContainerComponent extends Component {_x000D_
static propTypes = {_x000D_
children: childrenPropTypeLogic,_x000D_
}_x000D_
_x000D_
render() {_x000D_
return (_x000D_
<div>_x000D_
{this.props.children}_x000D_
</div>_x000D_
);_x000D_
}_x000D_
}_x000D_
_x000D_
_x000D_
_x000D_
class App extends Component {_x000D_
render(){_x000D_
return (_x000D_
<ContainerComponent>_x000D_
<div>1</div>_x000D_
<div>2</div>_x000D_
</ContainerComponent>_x000D_
)_x000D_
}_x000D_
}_x000D_
_x000D_
ReactDOM.render(<App /> , document.querySelector('section'))
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>_x000D_
_x000D_
<section />
_x000D_
The answers here don't seem to quite cover checking the children exactly. node
and object
are too permissive, I wanted to check the exact element. Here is what I ended up using:
oneOfType([])
to allow for single or array of childrenshape
and arrayOf(shape({}))
for single and array of children, respectivelyoneOf
for the child element itselfIn the end, something like this:
import PropTypes from 'prop-types'
import MyComponent from './MyComponent'
children: PropTypes.oneOfType([
PropTypes.shape({
type: PropTypes.oneOf([MyComponent]),
}),
PropTypes.arrayOf(
PropTypes.shape({
type: PropTypes.oneOf([MyComponent]),
})
),
]).isRequired
This issue helped me figure this out more clearly: https://github.com/facebook/react/issues/2979
Source: Stackoverflow.com