0

i use react-select on my dependent dropdownlist. Options2 will be dependent on Options1. i use filter() to display which Options2 will be showed up. Then i want to clear / reset the selectedOption2 eachtime selectedOption changed. I add my value props in order to understandable for you. When selectedOption is changed, selectedOption2 will be "Select..". i have tried to solve this code, but i can't.

import React, { Component } from "react";

import Select from "react-select";
import { options1, options2 } from "./data";

class Esensial extends Component {
  constructor() {
    super();
    this.state = {
      selectedOption: {},
      selectedOption2: {}
    };
  }

  handleChange1 = selectedOption => {
    this.setState({ selectedOption });
    this.setState({ selectedOption2: null });
  };

  handleChange2 = selectedOption => {
    this.setState({ selectedOption2: selectedOption });
  };
  render() {
   const options1 = [
      { value: "One", 
        label: "One" 
      },
      {
        value: "Two",
        label: "Two"
      }];
   const options2 = [
      {
        value: "One-A",
        label: "One-A",
        link: "One"
      },
      {
        value: "One-B",
        label: "One-B",
        link: "One"
      },
      {
        value: "Two-A",
        label: "Two-A",
        link: "Two"
      },
      {
        value: "Two-B",
        label: "Two-B",
        link: "Two"
       }];

    const filteredOptions = options2.filter(
      o => o.link === this.state.selectedOption.value
    );

    return (
      <div>
        <h1>UKM Esensial</h1>

        <div className="form-group">
          <label>Pilih Jenis Upaya Pelayanan Kesehatan</label>
          <Select
            className="form-control"
            isClearable={false}
            onChange={this.handleChange1}
            options={options1}
          />
        </div>
        <div className="form-group">
          <label>Pilih Variable</label>
          <Select
            className="form-control"
            isClearable
            onChange={this.handleChange2}
            options={filteredOptions}
          />
        </div>
      </div>
    );
  }
}
export default Esensial;

2 Answers 2

1

Firstly, making 2 separate setState calls is not a good idea. Secondly, I don't see the value prop set on Select elements. The following code should work:

import React, { Component } from "react";

import Select from "react-select";
import { options1, options2 } from "./data";

class Esensial extends Component {
  constructor() {
    super();
    this.state = {
      selectedOption: {},
      selectedOption2: {}
    };
  }

  handleChange1 = selectedOption => {
    this.setState({ selectedOption, selectedOption2: null });
  };

  handleChange2 = selectedOption => {
    this.setState({ selectedOption2: selectedOption });
  };
  render() {
    const filteredOptions = options2.filter(
      o => o.link === this.state.selectedOption.value
    );

    return (
      <div>
        <h1>UKM Esensial</h1>

        <div className="form-group">
          <label>Pilih Jenis Upaya Pelayanan Kesehatan</label>
          <Select
            className="form-control"
            isClearable={false}
            onChange={this.handleChange1}
            options={options1}
            value={this.state.selectedOption}
          />
        </div>
        <div className="form-group">
          <label>Pilih Variable</label>
          <Select
            className="form-control"
            isClearable
            onChange={this.handleChange2}
            options={filteredOptions}
            value={this.state.selectedOption2}
          />
        </div>
      </div>
    );
  }
}
export default Esensial;
Sign up to request clarification or add additional context in comments.

1 Comment

i set the value prop in outside class in "./data". the code works fine, but i want clear/reset the second select when i change the first select
1

I think the problem is that Select components don't know what you are actually selected. The Select component could render selected item by itself, but it won't remember what you selected later because you didn't explicitly tell it.

You should pass value prop to you Select components with the selectedOption and selectedOption2 from your state.

As Tushar Agarwal showed in the code example.

3 Comments

i set the value prop in outside class in "./data". the code works fine, but i want clear/reset the second select when i change the first select
I don't know where you set value prop in ./data. But I'm sure it won't affect Selects from this component. value prop should be explicitly set in this component for each Select. Right now Select components don't know about your changes in the handlers, you don't use your state values.
Hi Andrey, i follow Tushar advice, and it works correctly. Thak You.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.