<html>
<head>
<meta charset="UTF-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script>
<!---this is used to compile on run time--->
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
<h3>React Js Rendering a dynamic list of checkboxes using map method</h3>
<div id="app"></div>
<script type="text/babel">
const { useState } = React;
function App() {
const options = ["apple", "banana", "orange"];
const [checked, setChecked] = useState(new Array(options.length).fill(false));
const handleChange = (index) => {
const newChecked = [...checked];
newChecked[index] = !checked[index];
setChecked(newChecked);
}
return (
<div>
{options.map((option, index) => (
<div key={option}>
<input
type="checkbox"
checked={checked[index]}
onChange={() => handleChange(index)}
/>
<label>{option}</label>
</div>
))}
</div>
);
}
ReactDOM.render(<App />, document.getElementById('app'));
</script>
</body>
</html>