score:197

Accepted answer

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}
    />;
  }
});

score:0

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.

score:0

hey i might be late to jump in but i want to share a small tip. in case you are here for the reason that you want to pass props dynamically. then you can use * sign to import all the stuff as an alias , i.e in this case as grs then the imports will be in an object, then you can use that object to pass props dynamically. hope this will help some of you.

import * as grs from "../components/theme";
import react from "react";
const gridinput = ({ sp, ...props }) => {
  return (
    <grid {...grs[`gr${sp}`]}>
      <input {...props} />
    </grid>
  );
};
export default gridinput;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

score:1

also you can try this short hand way

 <child {...(this.props.editable  && { editable: this.props.editableopts })} />

score:11

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.

score:22

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

score:69

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

<child {...(this.props.editable ? {editable: this.props.editableopts} : {})} >

should work.


Related Query

More Query from same tag