1

I'm working on React JS project. I have 4 dropdown button (select option). And all the dropdown will be coming from DB dynamically. So wanted to know what is the right method to implement.

Initially I had only 1 dropdown box, so implemented it with ajax call and append the values with <option> tag under <select> tag. Now I have 3 more dropdown, so do I need to call multiple ajax calls for all 4 box ? or is there any other ways to implement it ?

Please do suggest here. Because I don't want to implement in wrong way and revert back again.

7
  • Unless they are stored in different places you should be able to get the required values with one Ajax call and then store the results in the application state. Commented May 23, 2017 at 7:59
  • Are all your dropdown values independent of each other? Also, why use JQuery with reactjs? Commented May 23, 2017 at 8:00
  • Yes all are different values independent of each other. Jquery it's just single function with ajax call.. Commented May 23, 2017 at 8:02
  • You can create a common component and just call it after you do an ajax call. It'll be much cleaner. And if each dropdown are different (has different options from different collections), then you would surely need to query for each of them. Commented May 23, 2017 at 8:03
  • 1
    why not returning all the data in one ajax call like this: {option1: [], option2: [], option3: [], option4: []} Commented May 23, 2017 at 8:17

1 Answer 1

2

If you create a small component for your dropdowns, like so:

import React, { Component } from 'react';

class SelectOption extends Component {
    render() {
        return (
            <option value={this.props.dataItem.key}>{this.props.dataItem.val}</option>
        )
    }
}

class SimpleDropdown extends Component {

    render() {

        let options = [];

        if (this.props.selectableData) {
            const selectableData = this.props.selectableData;
            options = selectableData.map((dataItem) =>
                <SelectOption key={'option_' + dataItem.key} dataItem={dataItem} />
            );
        }

        return (
            <div>
                <select onChange={this.props.handleInputChange} name={this.props.name} >
                    {options}
                </select>
            </div>
        )
    }
}

export default SimpleDropdown;

You can use it in your parent component, something like this...

import React, { Component } from 'react';

import SimpleDropdown from './common/SimpleDropdown';


class Parentextends Component {

    componentDidMount() {
        // here you handle your ajax call or calls, depending on what you choose to go with
    }

    handleInputChange = (e) => {

        const target = e.target;
        const value = target.type === 'checkbox' ? target.checked : target.value;
        const name = target.name;

        this.setState({
            [name]: value
        });
    }


    render() {

        const ajaxResultFirst = ajaxResult.First;
        const ajaxResultSecond = ajaxResult.Second;
        const ajaxResultThird = ajaxResult.Third;
        const ajaxResultFourth = ajaxResult.Fourth;

        return (
            <section className="form">

                    <SimpleDropdown
                        name="FirstDropdown"
                        selectableData={ajaxResultFirst}
                        handleInputChange={this.handleInputChange}
                    />
                    <SimpleDropdown
                        name="SecondDropdown"
                        selectableData={ajaxResultSecond}
                        handleInputChange={this.handleInputChange}
                    />
                    <SimpleDropdown
                        name="ThirdDropdown"
                        selectableData={ajaxResultThird}
                        handleInputChange={this.handleInputChange}
                    />
                    <SimpleDropdown
                        name="FourthDropdown"
                        selectableData={ajaxResultFourth}
                        handleInputChange={this.handleInputChange}
                    />

            </section>
        );
    }
};

export default Parent;

Something like this should work. But I still recommend using a different plugin than jquery for making ajax requests, my first choice is axios https://github.com/mzabriskie/axios.

Sign up to request clarification or add additional context in comments.

Comments

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.