This react hook extracts toggle behaviour so you can only focus on the appearance of your components.
import React from 'react';
import useToggle from '@dprovodnikov/use-toggle';
const Toggle = function () {
const handleChange = value => console.log('Toggle has changed its value to', value);
const [isOn, toggle] = useToggle(false, handleChange);
return (
<button onClick={toggle}>
{isOn ? 'Turn off' : 'Turn on'}
</button>
);
};
export default Toggle;
The hooks takes a default value as the first argument, and a change listener function as the second.
$ npm i @dprovodnikov/use-toggle
- React^16.8
MIT