0

I've got a <select> element where I'm pulling the options from a Rails data model. This is ok, but produces a bog-standard HTML select dropdown.

However, I'm wanting to use react-select component and this is where I'm struggling. I am able to render the react-select dropdown, but the options are blank. I don't have any errors in the console, and I can see the 51 items in my array in React-Dev-Tools.

This is the code that produces the basic HTML dropdown.

import React from 'react';
import axios from 'axios';
import Select from 'react-select';

class Country extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            countries: []
        }
    }

    getCountries() {
        axios.get(`/countries.json`)
            .then(res => {
                const countries = res.data;
                this.setState({ countries });
            })
            .catch(error => console.log(error))
    }

    componentDidMount() {
        this.getCountries()
    }

    render() {
        return (
            <div className="container">
                <select className="taskList">
                    {this.state.countries.map((country) => {
                        return (
                            <option key={country.id} value={country.id}>{country.country_name}</option>
                        )
                    })}
                </select>
            </div>
        )
    }
}

export default Country

This is the code I'm trying for the react-select, and doesn't work

import React from 'react';
import axios from 'axios';
import Select from 'react-select';

class Country extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            countries: []
        }
    }

    getCountries() {
        axios.get(`/countries.json`)
            .then(res => {
                const countries = res.data;
                this.setState({ countries });
            })
            .catch(error => console.log(error))
    }

    componentDidMount() {
        this.getCountries()
    }

    render() {
        return (
            let countryItems = this.state.countries.map((country) =>
                <option key={country.id} value={country.id}>{country.country_name}</option>
            );
        return (
            <div className="container">
                <label>Country</label>
                <Select id="country" name="coffee_beans[country_id]" options={countryItems} />
            </div>
        )
    }
}

export default Country

1 Answer 1

1

Your options to react-select component should be an array of objects:

const options = [
  { value: 'chocolate', label: 'Chocolate' },
  { value: 'strawberry', label: 'Strawberry' },
  { value: 'vanilla', label: 'Vanilla' }
];

Now you are passing an array of components. So instead of

let countryItems = this.state.countries.map((country) =>
  <option key={country.id} value={country.id}>
    {country.country_name} . 
  </option>
);

try something along this:

let countryItems = this.state.countries.map(country => ({ 
  value: country.id,
  label: country.country_name
});
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry I left out all of the same code for brevity, I've now included it all. You'll see I'm still pulling the array.
Thanks @sami That appears to have worked. I've got issues submitting the value to my db, but that's probably a separate issue.

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.