<div id="ex">1</div>
class App extends React.Component {

  constructor(props){
      super(props);
             
      this.state = { color: this.props.colors[0] };
      
      this.changeColor = this.changeColor.bind(this);
  }
  
  changeColor(e){
    this.setState( { color: e.target.value });
  }
 
  render(){          
    return <div>
          <p style={{backgroundColor: this.state.color}}>sample text</p>
          <select onChange={this.changeColor}>
             {this.props.colors.map(c => (<option value={c}>{c}</option>))}
          </select>
          </div>
    }
          
}

ReactDOM.render(
  <App colors={['red', 'green', 'blue']}/>,
  document.getElementById("ex")
);
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js
  2. https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js