I have 2 components.
- seatBooking(parent => Class Component)
- theater(child => functional Component)
Now in the theater component, there are few checkboxes. All I want to know is when these checkboxes are clicked how do I pass the name of the selected checkbox names into an array in the parent component. following is my code
Parent
import theater from './theater ';
export default class seatBooking extends Component{
render(){
return(
<theater />
)
}
}
Child
export default function theater(){
var seats = []
const [seatChecked, setSeatChecked ] = useState(false);
const handleOnChange = (e) => {
let isChecked = e.target.checked;
seats.push(e.target.name)
if(isChecked){
alert(seats);
}else{
seats = seats.filter((name) => e.target.name !== name);
alert("removed",e.target.name)
}
console.log(seats)
}
render()
return(
<input type="checkbox" id="c3" name="A3"
checked={seatChecked["c3"]} onChange={(e) => handleOnChange(e)}/>
)}