Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
How to connect react-select with react-chartjs-2/How to use a select dropdown with charts in react?

I have a react JSX component that makes an api call to the backend and fetches the data I require. This data is currently inside a react-select tag which allows me to display all the options inside a dropdown. I'm trying now to use this dropdown to render the selection using charts, specifically react-chartjs-2. I've been trying for quite a while but I honestly am at my wits end.

Here's what the api call with the select looks like:

export default class ------ extends component{

constructor(props, context) {
    super(props, context);
    this.state = {
      selectedOption: {},
      
      };
  }


  fetchData = (inputValue, callback) => {
    setTimeout(() => {
      fetch(
        "api" +
          inputValue,
        {
          method: "GET",
        }
      )
        .then((resp) => {
          return resp.json();
        })
        .then((data) => {
          const tempArray = [];
          if (data) {
            if (data.length) {
              data.forEach((element) => {
                tempArray.push({
                  label: `${element.campo}`,
                  value: element.valor,
                });
              });
            } else {
              tempArray.push({
                label: `${data.campo}`,
                value: data.valor,
              });
            }
          }
          callback(tempArray);
        })
        .catch((error) => {
          console.log(error, "error");
        });
    }, 1000);
  };

  onSearchChange = (selectedOption) => {
    if (selectedOption) {
      this.setState({
        selectedOption,
      });
    }
  };

Here's the actual select element and the chart inside the return section

    render(){


         return(


              <AsyncSelect
               id="valx"
               value={this.state.selectedOption}
               loadOptions={this.fetchData}
               placeholder="placehold"
               onChange={(e) => {
               this.onSearchChange(e);
               }}
               defaultOptions={true}
               />


         <div class="charts">
            <Bar 
             id="MyBarChart"
             data={{}}
             options={{}}
             />
                <Pie
                 id="MyPieChart"
                 data={{}}
                 options={{}}
                 />
           </div>

I have a really long way to go when it comes to React, I'm sure the solution will turn out to be rather simple. Any and all help is welcome, and thanks in advance.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!