I am trying to implement a enable and disable toggle button on a list of rows from a data map returned from a function to fetch results from a backend
issue is i need to set defaultChecked to whatever value of el.enabled is within the map of json data but have been un-successful
here is what the data map looks like
data = [
{
"id": 1,
"enabled": true,
},
{
"id": 2,
"enabled": false,
},
{
"id": 3,
"enabled": true,
},
{
"id": 4,
"enabled": false,
}
]
data is derived from below function that fetched value from API backend
const [data, setData] = useState([]);
const fetchData = async () => {
let json = await getData();
setData(json);
}
and here is the updateToggle function that enabled or disabled each row
const [statusMode, setStatusMode] = useState();
const updateToggle = async (rowID) => {
// rowID = el.id
const updateData = {'id': rowID}
if ( statusMode == true ) {
const request = await disable(updateData);
setStatusMode(false)
}
if ( statusMode == false ) {
const request = await enable(updateData);
setStatusMode(true)
}
};
and here is what i have that is not working but shows what am trying to achieve
{data.map((el, k) => {
return (
<div>
<div class="checkbox">
<label class="switch">
<input type="checkbox" defaultChecked={setStatusMode(el.enabled, k)} onClick={() => updateToggle(el.id, k)} />
</label>
</div>
</div>
);
})}
issue is happening right here defaultChecked={setStatusMode(el.enabled, k)} as i want to set the value to whatever i get from el.enabled for the specific row
error am getting at the moment is
Too many re-renders. React limits the number of renders to prevent an infinite loop.
how can i achieve this? or is there a better way to go about this?
useState.