javascript - Making first select <option> blank after mapping through array in JSXReact - Stack Overflow

render(){let { classes } = this.props;let list = classes.map((item, index) => {return (<option >

  render(){

    let { classes } = this.props;

    let list = classes.map((item, index) => {
      return (
        <option >{item}</option>
      )
    })


    return(
      <div className="filter-bar">

        <form className="sort-form">
          <div className="classSelect">
            <span>select class</span>
              <select name="classSelect" onChange={this.handleClassChange}}>
                <option selected="selected"  >Please choose class</option>
                {list}
              </select>
          </div>
        </form>
      </div>
    )
  }
};

i want to map over an array and use each element as options in select dropdown, and also have an extra blank option which defaults until dropdown is clicked.

at the moment, the extra i have is available in the list, but default is always first element from array, whereas i want the default to be "Please choose your class"

can somebody explain?

  render(){

    let { classes } = this.props;

    let list = classes.map((item, index) => {
      return (
        <option >{item}</option>
      )
    })


    return(
      <div className="filter-bar">

        <form className="sort-form">
          <div className="classSelect">
            <span>select class</span>
              <select name="classSelect" onChange={this.handleClassChange}}>
                <option selected="selected"  >Please choose class</option>
                {list}
              </select>
          </div>
        </form>
      </div>
    )
  }
};

i want to map over an array and use each element as options in select dropdown, and also have an extra blank option which defaults until dropdown is clicked.

at the moment, the extra i have is available in the list, but default is always first element from array, whereas i want the default to be "Please choose your class"

can somebody explain?

Share Improve this question asked Oct 29, 2019 at 13:31 Theo WrightTheo Wright 1232 silver badges11 bronze badges 1
  • assign a value to the default option and use this value as initial state – Dupocas Commented Oct 29, 2019 at 13:33
Add a ment  | 

1 Answer 1

Reset to default 5
  render(){

const classes = [
  'Lorem',
  'Ipsum',
  'dolor',
  'Sit',
  'ames'
]

let list = classes.map((item, index) => {
  return (
    <option >{item}</option>
  )
})


return(
  <div className="filter-bar">
    <form className="sort-form">
      <div className="classSelect">
        <span>select class</span>
          <select name="classSelect" onChange={this.handleClassChange.bind(this)} defaultValue="none">
            <option value="none" disabled hidden> 
            </option> 
            {list}
          </select>
      </div>
    </form>
  </div>
)

You can use a disabled and hidden <option> with value "none". Instead of using the selected attribute, you should then use defaultValue="none" on the parent <select> to conform with modern react requirements.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745365746a4624570.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信