javascript - how to populate options for a react select by fetching values from an api to be visible when you click on the selec

I am using react-select for drop downs in my application. Below is my requirement.Select a value from t

I am using react-select for drop downs in my application. Below is my requirement.

  • Select a value from the drop down for the first Select ponent(second Select is not rendered yet).
  • Basing on the selected value fetch the options for second Select ponent and render the second Select box.
  • Click in the text area of the second Select.

  • What is happening : I see No Options as the default drop down. I can see the values from the API only when I type something in the box and that matches the default filter criteria.

What I want to happen : It should display the values that we fetched from the API call.

const options = [{ label: "first", value: "first" }];
let options1 = [];

async function copyOptionsForAsync() {
  let response = await fetch("");
  let data = await response.json();
  data.forEach(element => {
    let dropDownEle = { label: element["title"], value: element };
    options1.push(dropDownEle);
  });
}

class App extends React.Component {
  constructor() {
    super();
    this.state = {
      isSelected: false
    };
  }
  handleOnchange = () => {
    this.setState({ isSelected: true });
    copyOptionsForAsync();
    console.log(options1);
  };
  render() {
    return (
      <div className="App">
        <Select
          name="option"
          options={options}
          onChange={this.handleOnchange}
        />
        {this.state.isSelected ? <App1 /> : null}
      </div>
    );
  }
}
class App1 extends React.Component {
  render() {
    return (
      <div className="App">
        <Select name="options2" options={options1} />
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

This is the link to codesandbox page. Can any one tell me how will I be able to display the options once I click on the select box.

I am using react-select for drop downs in my application. Below is my requirement.

  • Select a value from the drop down for the first Select ponent(second Select is not rendered yet).
  • Basing on the selected value fetch the options for second Select ponent and render the second Select box.
  • Click in the text area of the second Select.

  • What is happening : I see No Options as the default drop down. I can see the values from the API only when I type something in the box and that matches the default filter criteria.

What I want to happen : It should display the values that we fetched from the API call.

const options = [{ label: "first", value: "first" }];
let options1 = [];

async function copyOptionsForAsync() {
  let response = await fetch("https://jsonplaceholder.typicode./todos");
  let data = await response.json();
  data.forEach(element => {
    let dropDownEle = { label: element["title"], value: element };
    options1.push(dropDownEle);
  });
}

class App extends React.Component {
  constructor() {
    super();
    this.state = {
      isSelected: false
    };
  }
  handleOnchange = () => {
    this.setState({ isSelected: true });
    copyOptionsForAsync();
    console.log(options1);
  };
  render() {
    return (
      <div className="App">
        <Select
          name="option"
          options={options}
          onChange={this.handleOnchange}
        />
        {this.state.isSelected ? <App1 /> : null}
      </div>
    );
  }
}
class App1 extends React.Component {
  render() {
    return (
      <div className="App">
        <Select name="options2" options={options1} />
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

This is the link to codesandbox page. Can any one tell me how will I be able to display the options once I click on the select box.

Share Improve this question asked Sep 12, 2018 at 8:03 Sachin KumarSachin Kumar 4733 gold badges13 silver badges32 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

'App1' is rendering before you actually get the data. One way to fix this is to wait for the data to be fetched then render 'App1', like this:

handleOnchange = async () => {
   await copyOptionsForAsync();
   this.setState({ isSelected: true });
};

Working example in codesandbox: https://codesandbox.io/s/m6wr8zvjj

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信