How To Change This State With Radio Buttons
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.
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
})
}
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();
}
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?