Q:

How to apply an id attribute to a child element of a ReactJS component?

So I have a dropdown component:
import React from 'react';
import { PropTypes } from 'prop-types';

export default class Dropdown extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      value: this.props.selectedDropdownValue,
    };

    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(e) {
    this.setState({
      value: e.target.value,
    });

    this.props.onChange(e.target.value);
  }

  render() {
    let disabled = false;
    let dropdownOptions = null;
    const { options } = this.props;

    // Disable dropdown is the disabled attribute is added
    if (options) {
      dropdownOptions = this.props.options.map((option) =>
        <option value={option.key} key={option.key} >{option.display}</option>,
      );
    } else {
      // Setting default options
      const defaultOptions = [
        { key: '', display: 'Please configure defaultOptions' },
      ];

      disabled = true;

      dropdownOptions = defaultOptions.map((option) =>
        <option value={option.key} key={option.key} >{option.display}</option>,
      );
    }

    return (
      <select
        value={this.state.value}
        onChange={this.handleChange}
        disabled={disabled}
      >
        { dropdownOptions }
      </select>
    );
  }
}

Dropdown.propTypes = {
  options: PropTypes.arrayOf(PropTypes.object),
  onChange: PropTypes.func,
  selectedDropdownValue: PropTypes.string,
};

Dropdown.defaultProps = {
  options: undefined,
  onChange: this.handleChange,
  selectedDropdownValue: '',
};

And here is how I'm rendering the dropdown in my view:
import React from 'react';
import { PropTypes } from 'prop-types';
import Dropdown from 'components/Dropdown';

class Anish extends React.Component {
  render() {
    return (
      <div>
        <div>
          Value Selected: {this.props.selectedDropdownValue}
        </div>
        <label htmlFor={'dropdownId1'}>Sort by: </label>
        <Dropdown
          id="dropdownId1"
          options={[
            { key: '', display: 'Please select a value' },
            { key: 'NJ', display: 'New Jersey' },
            { key: 'NY', display: 'New York' },
          ]}
          onChange={this.props.onSetDropdownValue}
        />
      </div>
    );
  }
}

Anish.propTypes = {
  selectedDropdownValue: PropTypes.string,
  onSetDropdownValue: PropTypes.func.isRequired,
};

Anish.defaultProps = {
  selectedDropdownValue: '',
};

export default Anish;

Here is how the dropdown and label are rendered in the browser:

So how can I apply the id that's on <Dropdown /> to the <select> inside of it?
Any help is appreciated.

A:

props will contain the ID from the Dropdown component, but since you're within a React component use this.props.
return (
  <select
    id={this.props.id}
    value={this.state.value}
    onChange={this.handleChange}
    disabled={disabled}
  >
    {dropdownOptions}
  </select>
)

