Ad

How To Change This State With Radio Buttons

- 1 answer

I have a few selectable buttons that I want to change by selecting any of these state radio buttons I want to use as a parameter to send information to the api

import React ,{Component} from 'react';
import axios from "axios/index";

export default class Test extends Component {

    constructor(props) {
        super(props);
        this.state = {
            TypeStatus: '',
            OperatingStatus: '',
            RegistrationStatus: '',
        }
    }

this is my axios codes

componentDidMount(){
    axios({
        method: 'post',
        url: 'https://test.ir/api/registerANewAdd',
        data: {
            TypeStatus: '',
            OperatingStatus: '',
            RegistrationStatus: '',
        }
    })
    .then(function (response) {
        console.log(response);
    })
    .catch(function (error) {
        console.log(error);
    });
}

and this is my buttons

render() {
    return (
        <div className="container-fluid">
            <div className="row RegisterOptions">
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="TypeStatus" id="Justice" />
                        options1-1
                    </label>
                </div>
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="TypeStatus" id="Credit" />
                        options1-2
                    </label>
                </div>
            </div>
            <div className="row RegisterOptions">
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="OperatingStatus" id="New" />
                        options2-1
                    </label>
                </div>
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="OperatingStatus" id="LowPerformance" />
                        options2-2
                    </label>
                </div>
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="OperatingStatus" id="Old" />
                        options2-3
                    </label>
                </div>
            </div>
            <div className="row RegisterOptions">
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="RegistrationStatus" id="Unregistered" autoComplete="off" />
                        options3-1
                    </label>
                </div>
                <div className="col" data-toggle="buttons">
                    <label className="RegisterButtons">
                        <input type="radio" name="RegistrationStatus" id="Registered" />
                        options3-2
                    </label>
                </div>
            </div>
        </div>
    );
}

Please help. I would like to click on any of these buttons to add a class to its lable and each of these groups is just an option.

Ad

Answer

You should provide the value and onChange props to your radio input.

<div className="row RegisterOptions">
    <div className="col" data-toggle="buttons">
        <label className="RegisterButtons">
            <input type="radio" name="TypeStatus" id="Justice" value="Justice" onChange={this.changeHandle}/>
            options1-1
        </label>
    </div>
    <div className="col" data-toggle="buttons">
        <label className="RegisterButtons">
            <input type="radio" name="TypeStatus" id="Credit" value="Credit" onChange={this.changeHandle}/>
            options1-2
        </label>
    </div>
</div>

Here your value will change as per radio input, but changeHandle will remain same for all the radio input's.

Your changeHandle should be this,

changeHandle = (e) => {
   this.setState({
     [e.target.name]: e.target.value
   })
}

Demo


You can then use values from state for your axios call. You should make a separate function for your API call

callAPI = () => {
    axios({
        method: 'post',
        url: 'https://test.ir/api/registerANewAdd',
        data: {
            TypeStatus: this.state.TypeStatus,
            OperatingStatus: this.state.OperatingStatus,
            RegistrationStatus: this.state.RegistrationStatus,
        }
    })
    .then(function (response) {
        console.log(response);
    })
    .catch(function (error) {
        console.log(error);
    });
}

Now this function can be called from anywhere like on the click of submit button,

<button onClick={this.callAPI}>Submit</button>

or from componentDidMount,

componentDidMount(){
   this.callAPI();
}
Ad
source: stackoverflow.com
Ad