If you want to set placeholder for dropdown in reactjs? Reason is, React provide a better way of controlling the element by using states, so instead of using selected with option use the value property of select and define its value in the state variable, use the onChange method to update the state, Use it in this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class App extends React.Component { constructor(props) { super(props); this.state = { value: '1' }; } render(){ return( <select value={this.state.value} onChange={(e)=>{this.setState({value: e.target.value})}}> <option value='1' disabled>Select</option> { [2,3,4].map((i,j)=>{ return <option key={i} value={i}>{i}</option> }) } </select> ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.