简体   繁体   中英

PropTypes check for specific children eg. <li>, <h1>, etc

Let's say, I have the following simple Component:

const List = ({ children, ...props }) => (
  <ul {...props}>
    {children}
  </ul>
);

The corresponding propTypes is the following:

List.propTypes = {
  children: PropTypes.node
};

How can I make the propTypes be more specific?
So that it could accept one or more <li> (with any content within <li> ), but not <div> for example?

Like these examples:

<List>
  <li>Singe item</li>
</List>

<List>
  <li>First item</li>
  <li>Second item</li>
</List>

Thank you for your help!

Great question, Keeping it simple. maybe you should just try using Fragment. For example

<List>
 <Fragment>
  <li>First item</li>
  <li>Second item</li>
 </Fragment>
</List>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM