Populate 2nd dropdown based on selection from 1st dropdown in React.Js

前端 未结 3 1137
刺人心
刺人心 2021-02-06 19:11

I am learning react and struggling to get a second dropdown to populate based on which option is clicked from first dropdown.

I have included my code below.

I

相关标签:
3条回答
  • 2021-02-06 19:41
    1. event handlers for e.g. onClick must be a function
    2. this.setState expects an object, which will be merged with the state so the part where you set selected in your state must be

      <option key={i} onClick={() => {
          this.setState({selected: param.tableName})
      }>{param.tableName}}}</option>
      
    3. you use an undefined variable in your filter (selected), you have to use .filter(({tableName}) => tableName === this.state.selected) instead

    0 讨论(0)
  • 2021-02-06 19:44

    Some things to look at:

    The syntax in your call to setState in your <option> onClick event handler looks incorrect.

    You also make try to reference this.state.selected in your .filter condition without the path to the variable (this.state)


    Suggested solution(s):

    // The .setState method expects an object to be passed to it rather
    // than an expression that mutates the state directly
    
    // Pass it an object containing the properties you want to change,
    // ensure your use colon (':') rather than equals sign ('=') to set property:
    <option key={i} onClick={
      this.setState({
        selected: param.tableName
      })
    </option>
    
    
    // In the <select> tag,
    // you need to refer to the selected property in it as this.state.selected:
    
    // Don't forget the closing select tag, it's missing from your snippet
    <select>
    {
      this.state.params
        .filter(tableName => (tableName === this.state.selected))
        .map(columns => columns.map(col => <option>{col}</option>))
    }
    </select>
    
    0 讨论(0)
  • 2021-02-06 19:53

    You're doing it wrong way. You cannot call onClick method on <option>. Instead you should use onChange method on <select> (see react docs) like this:

    <select onChange={this.handleChange} >
      <option>1</option>
      <option>2</option>
      <option>3</option>
    </select>
    

    Then on 'onChange' event you can set your state to the selected option. Let's understand this scenario with an example.

    Suppose you have two dropdowns. One for showing companies and one for jobs corresponding to each company. Each company has its own set of jobs like this:

     companies:[
        { name: 'company1',  jobs: ['job1-1', 'job1-2', 'job1-3']},
        { name: 'company2', jobs: ['job2-1', 'job2-2', 'job2-3']},
        { name: 'company3', jobs: ['job3-1', 'job3-2', 'job3-3']}
      ]
    

    Now you have to just set a state, lets say 'selectedCompany' and then filter the companies array by 'selectedCompany'. Then you can just get that particular company object and map your 'jobs' dropdown by iterating over jobs array inside the company object.

    Here is the code snippet: https://codepen.io/madhurgarg71/pen/pRpoMw

    0 讨论(0)
提交回复
热议问题