As a React beginner exercice I have a component checkbox for a list of devices. If a device is checked, I want my App parent element to display the name of the device. I tried several ways to do it but cannot achieve it.
Here is my code
Checkbox.jsx
import { useState, useEffect } from "react"
const Checkbox = ({ label, name, sendData}) => {
const [isChecked, setIsChecked] = useState(false);
const handleChange =() => {
setIsChecked({isChecked: !isChecked} )
}
useEffect(()=> {
sendData(isChecked)
})
return (
<div className="checkbox-wrapper">
<label>
<input
type="checkbox"
checked={isChecked }
onChange={handleChange}
name={name}
/>
<span>{label}</span>
</label>
</div>
);
};
export default Checkbox;
App.js
import { useState } from 'react';
import Checkbox from './components/Checkbox';
import './App.css';
import { useState } from 'react';
import Checkbox from './components/Checkbox';
import './App.css';
function App() {
const [ownDevice, setOwnDevice] = useState(false)
const sendData = (childData) => {
setOwnDevice(childData)
}
const displayDevice = (Checkbox) => {
if (ownDevice) {
return Checkbox.name
}
}
return(
<div className="App">
<h1>What devices do you own?</h1>
<Checkbox label="laptop" name="laptop" sendData={sendData} />
<Checkbox label="desktop" name="desktop" />
<Checkbox label="phone" name="phone" />
<p>{displayDevice}</p>
</div>
)
}
export default App;
2
Answers
I have solved your problem this way, hope it helps:
CheckBox file :
First of all, the code you have written will be stuck in an infinite render loop if we update the parent state on changing the
isChecked
state because you haven’t provided a dependency touseEffect
which means it will update the parent state every time your component renders.You need to remove the
useEffect
altogether. And secondly you can simple pass thesetOwnDevice
method directly to the child. The child will update the parent state which will result in a rerender. We can then use that state to display the selected device. The updatedCheckbox.jsx
component will look like this:Then update the
App.js
file:This could still be improved further because currently it won’t show two checked devices at the same time.