A
A
Andrew2017-08-17 20:28:43
React
Andrew, 2017-08-17 20:28:43

How can you conditionally pass props or not?

// константа от которой зависит передавать пропс defaultValue или нет
const needDefaultValue = false;
<input
  defaultValue={1}
></input>

how can this be done?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Wolf, 2017-08-17
@undefined_title

const needDefaultValue = false;

React.createElement('input', needDefaultValue ? { defaultValue: i } : {});
// или
const params = {};
if(needDefaultValue) params.defaultValue = i;
<input {...params} />;

R
Roman Alexandrovich, 2017-08-17
@RomReed

The question is a little unclear. If you want to set the default value of the prop, then it is done like this

class AddAddressComponent extends React.Component {
  render() {
    let {provinceList,cityList} = this.props
    if(cityList === undefined || provinceList === undefined){
      console.log('undefined props')
    } else {
      console.log('defined props')
    }

    return (
      <div>rendered</div>
    )
  }
}

AddAddressComponent.defaultProps = {
  cityList: [],
  provinceList: [],
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question