简体   繁体   中英

Initialising defaultValue with select field in reactjs

<select defaultValue={(default_value !== undefined) ? default_value : ""} >

If defaultValue is "", will it select the option which has a value of "" or will it select the first one as the default?

I want to select the first one as default, if default_value is undefined.

If you give undefined in defaultValue, then it will select first option as default, if you give '' or any value, it will search for that and select that option, but if it don't find that value it will select the first option. Try this:

class Logo extends React.Component {
        render(){
            let defaultValue = '';
            return (
                <select defaultValue={defaultValue} >
                   <option value='a'>a</option>
                   <option value='b'>b</option>
                   <option value='c'>c</option>
                   <option value='d'>d</option>
                </select>
            );
       }
}

check the jsfiddle : https://jsfiddle.net/cvw7h1th/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM