📅  最后修改于: 2022-03-11 15:03:17.502000             🧑  作者: Mango
import { useState, useEffect } from 'react';
import { PropTypes } from 'prop-types';
function Switch(props) {
const [toggle , setToggle ] = useState(false)
const toggleClass = 'transform translate-x-12';
const _handleClick = e => {
props.cb()
setToggle(!toggle);
}
useEffect(() => {
setToggle(props.toggled)
}, [props.toggled])
return (
// Switch Container
{
_handleClick();
}}
>
{/* Switch */}
{!toggle && (
{props.offValue}
)}
{toggle && (
{props.onValue}
)}
);
}
Switch.PropTypes = {
cb: PropTypes.func.isRequired,
toggled: PropTypes.bool.isRequired,
offValue: PropTypes.string.isRequired,
onValue: PropTypes.string.isRequired
}
export default Switch;