what I'm trying to do is simple. I have two buttons, one that says toggle on and one that says toggle off. When I click one of them, I want to set my state to true or false (depending on which button was clicked) and re render, using the newly set true false values. But for some reason, rows.map is not a function. Here is what I have so far:
function Calculator() {
const [rows, setRows] = useState([
{
rowVal: 0,
editing: false,
someVal: 20
},
{
rowVal: 1,
editing: true,
someVal: 20
},
])
return(
<div className="container">
<table className="table">
<thead>
<tr>
<th className="text-center" scope="col">SomeVal</th>
</tr>
</thead>
<tbody>
{
const togOff = <button onClick={setRows({editing: false})}>Toggle Off</button>
const togOn = <button onClick={setRows({editing: true})}>Toggle On</button>
rows.map(val => {
if (val.editing === true){
togOff
} else {
togOn
}
})
}
</tbody>
</table>
</div>
)
}
If anyone can point me in the right direction, that would be great.
rows.map(({ editing }) => editing ? togOff : togOn)saves you space. best to move yourconstdeclerations out of your JSX too. also youronClickmethods are being immediately invoked. wrap your calls in a functiononClick={() => setRows({editing: false})}