i passed the handleclick function to the onclick because i want to run the two function in onclick event but the first one (setEnabled) doesn’t run
import { useState } from "react";
export default function Togglelang(props) {
const [enabled, setEnabled] = useState(false);
const isenglish = props.isenglish
const setisenglish = props.setisenglish
const handleClick = () => {
setEnabled(!enabled);
setisenglish(!isenglish);
};
return (
<div className="relative flex flex-col items-center justify-center w-fit h-fit overflow-hidden">
<div className="flex">
<label className="inline-flex relative items-center mr-5 cursor-pointer">
<input
type="checkbox"
className="sr-only peer"
checked={enabled}
readOnly
/>
<div
onClick={handleClick}
className="w-11 h-6 bg-lightGrey rounded-full peer peer-focus:ring-green-300 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-0.5 after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-blue"
></div>
</label>
</div>
</div>
);
}
at first i wrote the both function in onclick but i try to put them in a single function aand pass that to onclick but it still doesn’t work
2
Answers
Looks like it is running to me
https://codesandbox.io/s/flamboyant-pare-pjk55s?file=/src/App.js
Press the button and look in the console, you see the
setisenglish
function is console logging, and the enabled state is changingYou should try console logging the
enabled
state to see ifsetEnabled
is running. (Also, I wouldn’t recommend adding anonClick
on adiv
. If what you want here is a button, try thebutton
element). One more thing, there is no tailwindCSS property namedbg-lightGrey
, change the div to some other color and make sure that the div is not hidden.