admin管理员组文章数量:1405102
I'm trying to use radio buttons in React (that I always used) but this time with react-bootstrap
.
I have two radio buttons and I want just one to be checked and get that value.
import React, { useState } from "react";
import { Form, Button } from "react-bootstrap";
const StandAdd = () => {
const [item, setItem] = useState({
kindOfStand: ""
});
const { kindOfStand } = item;
const handleInputChange = event => {
event.preventDefault();
setItem({ ...item, [event.target.name]: event.target.value });
};
const handleSubmit = event => {
event.preventDefault();
alert(`${kindOfStand}`);
};
return (
<form onSubmit={handleSubmit}>
<Form.Group controlId="kindOfStand">
<Form.Check
type="radio"
aria-label="radio 1"
label="Design"
onChange={handleInputChange}
checked={kindOfStand === "design"}
// value="design" // <-- once enabled I can't select it
/>
<Form.Check
type="radio"
aria-label="radio 2"
label="Food"
onChange={handleInputChange}
checked={kindOfStand === "food"}
// value="food" // <-- once enabled I can't select it
/>
</Form.Group>
<Button variant="primary" type="submit">
Submit
</Button>
</form>
);
};
export default StandAdd;
Codesandbox link:
any idea on how to get the value?
I'm trying to use radio buttons in React (that I always used) but this time with react-bootstrap
.
I have two radio buttons and I want just one to be checked and get that value.
import React, { useState } from "react";
import { Form, Button } from "react-bootstrap";
const StandAdd = () => {
const [item, setItem] = useState({
kindOfStand: ""
});
const { kindOfStand } = item;
const handleInputChange = event => {
event.preventDefault();
setItem({ ...item, [event.target.name]: event.target.value });
};
const handleSubmit = event => {
event.preventDefault();
alert(`${kindOfStand}`);
};
return (
<form onSubmit={handleSubmit}>
<Form.Group controlId="kindOfStand">
<Form.Check
type="radio"
aria-label="radio 1"
label="Design"
onChange={handleInputChange}
checked={kindOfStand === "design"}
// value="design" // <-- once enabled I can't select it
/>
<Form.Check
type="radio"
aria-label="radio 2"
label="Food"
onChange={handleInputChange}
checked={kindOfStand === "food"}
// value="food" // <-- once enabled I can't select it
/>
</Form.Group>
<Button variant="primary" type="submit">
Submit
</Button>
</form>
);
};
export default StandAdd;
Codesandbox link: https://codesandbox.io/s/react-bootstrap-radio-button-1d443
any idea on how to get the value?
Share edited Jun 29, 2020 at 18:19 kboul 14.6k5 gold badges47 silver badges58 bronze badges asked Jun 24, 2020 at 9:07 Marco DiscoMarco Disco 5651 gold badge8 silver badges25 bronze badges1 Answer
Reset to default 4Give a value
attribute to the Form.Check
s and then get the value using e.target.value
once you click the radio btn like this. Use e.persist();
to avoid getting the following error:
This synthetic event is reused for performance reasons. If you're seeing this, you're accessing the method currentTarget on a released/nullified synthetic event
const StandAdd = () => {
const [item, setItem] = useState({ kindOfStand: "", another: "another" });
const { kindOfStand } = item;
const handleChange = e => {
e.persist();
console.log(e.target.value);
setItem(prevState => ({
...prevState,
kindOfStand: e.target.value
}));
};
const handleSubmit = e => {
e.preventDefault();
alert(`${kindOfStand}`);
};
return (
<form onSubmit={handleSubmit}>
<Form.Group controlId="kindOfStand">
<Form.Check
value="design"
type="radio"
aria-label="radio 1"
label="Design"
onChange={handleChange}
checked={kindOfStand === "design"}
/>
<Form.Check
value="food"
type="radio"
aria-label="radio 2"
label="Food"
onChange={handleChange}
checked={kindOfStand === "food"}
/>
</Form.Group>
<Button variant="primary" type="submit">
Submit
</Button>
</form>
);
};
Demo
本文标签: javascriptReact with bootstrap and Radio buttonsStack Overflow
版权声明:本文标题:javascript - React with bootstrap and Radio buttons - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744882792a2630307.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论