Can a React prop type be defined recursively?

后端 未结 3 1550
萌比男神i
萌比男神i 2020-12-16 14:25

Suppose we\'re defining a React class that will display a tree.

React.createClass({
    propTypes: {
        tree: treeType
    },
    render: function () {
         


        
3条回答
  •  执念已碎
    2020-12-16 14:59

    I created recursive prop types something like this and it worked for me. Let me know if it works for you as well. lazyTreeType function will be called as many times as there is a sub in the object.

    const lazyTreeType = () => some_props;
    const some_props= PropTypes.shape({
      date: PropTypes.string,
      updated: PropTypes.string,
      name: PropTypes.string,
      sub: PropTypes.arrayOf(lazyTreeType),
      type: PropTypes.string,
    });
    
    const component_proptype = PropTypes.shape({
      id: PropTypes.string,
      sub: PropTypes.arrayOf(some_props),
    });
    

提交回复
热议问题