Im trying to pass in a method and its needed parameters with "onClick" but it wont work.
I read you can do it like this : onClick={() => handleSort(column)} but also that the problem with using an arrow function in the render call is it will create a new function every time, which ends up causing unneeded re-renders.
function ToggleSwitch() {
const [values, setValues] = React.useState({
astma: "off",
});
function toggle (nameIn, valueIn){
console.log("Toggle")
const {name, value} = (nameIn, valueIn);
if (value === "off") {
setValues({
...values,
[name]: "on",
});
} else if (value === "on") {
setValues({
...values,
[name]: "off",
});
}
};
//return just the function and make the html in the page and on klick use the toggle function
//remember to export values as well.
return (
<div
className={`switch ${values.astma}`}
onClick={toggle("astma", values.astma)}
id={"astma"}
name={"astma"}
/>
);
}
ReactDOM.render(<ToggleSwitch />, document.getElementById("root"));
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>