Search code examples
reactjsreact-proptypes

PropType for component prop ReactJS


Trying to get a clear answer on how to assign propTypes for React Components passed as a prop.

Is it even necessary that I check this prop type, and if so, how would I go about using proptypes for the following exapmle:

import React from "react";
import PropTypes from "prop-types";

import styles from "./NavIcons.module.css";

function NavIcons(props) {
  return (
      <div className={styles.navIcons}>{props.children}</div>
  );
}

NavIcons.propTypes = {
  children: // WHAT GOES HERE?
};

export default NavIcons;

Solution

  • It's going to be one or more nodes -

    NavIcons.propTypes = {
      children: PropTypes.oneOfType([
        PropTypes.arrayOf(PropTypes.node),
        PropTypes.node
      ]),
    };