I have used the antd Radio Group
and I want to call a function when the user clicks on the options, no matter the option is selected or not.
I used the onChange
but it works only in changing selected option cases.
Suppose I have this options in my radio group: A,B,C. Now A is selected. I want to console.log("A")
when the user clicks on A. if the user clicks on A twice, I want to console.log twice.
import { Radio } from 'antd';
const RadioButton = Radio.Button;
const RadioGroup = Radio.Group;
function onChange(e) {
console.log(`radio checked:${e.target.value}`);
}
ReactDOM.render(
<div>
<div>
<RadioGroup onChange={onChange} defaultValue="a">
<RadioButton value="a">Hangzhou</RadioButton>
<RadioButton value="b">Shanghai</RadioButton>
<RadioButton value="c">Beijing</RadioButton>
</RadioGroup>
</div>
</div>
, mountNode);
You may use onClick
, but it's unavailable to RadioGroup
component. Hence, put it into each of the Radio.Button
components would be a choice.
function onClick(e) {
console.log(`radio clicked:${e.target.value}`);
}
ReactDOM.render(
<div>
<div>
<RadioGroup onChange={onChange} defaultValue="a">
<RadioButton onClick={onClick} value="a">Hangzhou</RadioButton>
<RadioButton onClick={onClick} value="b">Shanghai</RadioButton>
<RadioButton onClick={onClick} value="c">Beijing</RadioButton>
</RadioGroup>
</div>
</div>
, mountNode);