React: inline conditionally pass prop to component

后端 未结 6 2115
感情败类
感情败类 2020-12-07 19:35

I would like to know if there is a better way to conditionally pass a prop than using an if-statement.

For example, right now I have:

var parent = R         


        
6条回答
  •  时光取名叫无心
    2020-12-07 20:16

    var parent = React.createClass({
      propTypes: {
        editable: React.PropTypes.bool.isRequired,
        editableOpts: React.PropTypes.shape({...})
      },
      render: function() {
        return (
          
        );
      }
    });
    

    This passes the props if they are defined. Else the props are not passed. In the other answer's the props are still passed but the value is undefined which still means the props are being passed.

提交回复
热议问题