React: inline conditionally pass prop to component

后端 未结 6 2112
感情败类
感情败类 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 (
          <Child 
            {...(this.props.editable && {editable=this.props.editableOpts})} 
          />
        );
      }
    });
    

    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.

    0 讨论(0)
  • 2020-12-07 20:17

    You were close with your idea. It turns out that passing undefined for a prop is the same as not including it at all, which will still trigger the default prop value. So you could do something like this:

    var parent = React.createClass({
      propTypes: {
        editable: React.PropTypes.bool.isRequired,
        editableOpts: React.PropTypes.shape({...})
      },
      render: function() {
        return <Child 
          editable={this.props.editable ?
                      this.props.editableOpts : 
                      undefined}
        />;
      }
    });
    
    0 讨论(0)
  • 2020-12-07 20:19

    Define props variable:

    let props = {};
    if (this.props.editable){
      props.editable = this.props.editable;
    }
    

    And then use it in JSX:

    <Child {...props} />
    

    Here is a solution in your code:

    var parent = React.createClass({
      propTypes: {
        editable: React.PropTypes.bool.isRequired,
        editableOpts: React.PropTypes.shape({...})
      },
      render: function() {
        let props = {};
        if (this.props.editable){
          props.editable = this.props.editable;
        }
        return (
          <Child {...props} />
        );
      }
    });
    

    Source, React documentation: https://facebook.github.io/react/docs/jsx-in-depth.html#spread-attributes

    0 讨论(0)
  • 2020-12-07 20:23

    Actually, if your prop is boolean it isn't needed to implement condition but if you wanna add prop by inline condition you should write like below:

    const { editable, editableOpts } = this.props;
    return (
      <Child {...(editable && { editable: editableOpts } )} />
    );
    

    Hope it doesn't confuse you. the {... means it is spread operator like passing existed props: {...props} and the editable && means if editable is true the { editable: editableOpts } object will make and with {... we will make a new object like it: {...{ editable: editableOpts }} that it means editable={editableOpts} but if this.porps.editable is true.

    0 讨论(0)
  • 2020-12-07 20:24

    Add a spread operator to the this.props.editable:

    <Child {...(this.props.editable ? {editable: {this.props.editableOpts}} : {})} >
    

    should work.

    0 讨论(0)
  • 2020-12-07 20:33

    also you can try this short hand way

     <Child {...(this.props.editable  && { editable: this.props.editableOpts })} />
    
    0 讨论(0)
提交回复
热议问题