I am trying to define my list of PropTypes for a class component. The component can accept either a regular DOM element or window
as its prop value. Currently, I have:
class FooContainer extends Component {
static propTypes = {
container: PropTypes.oneOfType([
PropTypes.element,
PropTypes.instanceOf(Element),
]),
};
static defaultProps = {
container: window,
};
constructor(props: any) {
super(props);
}
render() {
return (
<div />
);
}
}
However, when the component defaults to using window
as the container prop, the following error is thrown:
Warning: Failed prop type: Invalid prop `container` supplied to `FooContainer`.
As such, what is the valid PropType to use for property with value of window
?
What you can do is use oneOfType
and include an instance of the window
object's constructor, Window
:
PropTypes.instanceOf(window.constructor)
This will allow any Window
instance as a prop value.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments