steveruizok
4/26/2019 - 4:29 PM

Switch Tutorial Controlling State From Props

Switch Tutorial Controlling State From Props

import * as React from "react"
import { Frame } from "framer"

type Props = {
    isOn: boolean
    onValueChange: (isOn: boolean) => any
}

export function Switch(props: Props) {
    const [state, setState] = React.useState({
        isOn: false,
    })

    React.useEffect(() => {
        setState({
            isOn: props.isOn,
        })
    }, [props.isOn])

    const handleTap = () => {
        setState({
            isOn: !state.isOn,
        })
    }

    return (
        <Frame size="100%" onTap={handleTap}>
            {state.isOn.toString()}
        </Frame>
    )
}

Switch.defaultProps = {
    isOn: false,
    onValueChange: isOn => window.alert(isOn),
}