1

I have a component and I want to pass down an unknown array of objects (with unknown length) to it and display it as attributes in key and value format in the inner input tag.

<Input 
  type="text"
  label = {"length" + '(' + this.state.package_length_unit + ')'}
  placeholder = "length"
  defaultValue = {this.state.package_length ? this.state.package_length : ''}
  onInputChange = { this.onInputChange }
  attrs = {[{name: 'id',value: 'package_length'},{name: 'apikey',value: 'package_length'}]}
/> 

inside the component I have something like this

<input 
  type = {props.type} 
  ref = {this.inputRef}
  className = {inputClassName}
  disabled = {props.disabled ? 'disabled' : ''}
  defaultValue = {props.defaultValue ? props.defaultValue : ''}
  onChange = {props.onInputChange ? props.onInputChange : e => {}}
  onKeyUp = {this.handleTyping}
  placeholder = {
     props.placeholder ? props.placeholder : ''
  } 
  {...props.attrs} // ??
  // ********* here I want to have ==> 
  // id = 'package_length'
  // apikey = 'package_length'
  // ********* printed
 />

is there a way to achieve this? Thanks in advance.

0

1 Answer 1

1
{
  ...props.attrs.reduce((prev, curr) => {
    prev[curr.name] = curr.value;

    return prev;
  }, {})
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.