<html>
<head>
<meta charset="UTF-8" />
<script src="https://unpkg.com/react@17.0.2/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone@7.14.7/babel.min.js"></script>
</head>
<body>
<div id="app"></div>
<script type="text/babel">
const { useState } = React;
function App() {
const [options] = useState([
"Laptop",
"Mobile",
"Tablet",
"Mouse",
"Desktop",
"Monitor",
"CPU",
]);
const [selectedValue, setSelectedValue] = useState("Tablet");
const handleOptionChange = (event) => {
setSelectedValue(event.target.value);
};
return (
<div>
<h3>React js Radio button Default checked/selected</h3>
<div className="radio-group">
{options.map((option, index) => (
<label htmlFor={index} key={index}>
<input
type="radio"
id={index}
value={option}
checked={selectedValue === option}
onChange={handleOptionChange}
/>
{option}
</label>
))}
</div>
<pre>Selected Value: {selectedValue}</pre>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>
<style>
.radio-group {
display: flex;
flex-direction: column;
}
label {
display: flex;
align-items: center;
margin-bottom: 10px;
font-size: 16px;
}
input[type="radio"] {
margin-right: 10px;
}
input[type="radio"]:checked+span {
font-weight: bold;
}
pre {
font-size: 16px;
margin-top: 10px;
}
</style>
</body>
</html>