mardi 6 novembre 2018

Controlled Component Checkbox in a Stateless Component

I have a stateless component which gets props passed down from its parend

const Foo = ({ isChecked, }) => {
    function _onChange() {
        alert('clicked')
    }

    return (
        <label>
            <input
                type='checkbox'
                onChange={_onChange.bind(this)}
                checked={isChecked} />
        </label>
     )

How do I check/uncheck the checkbox based on the prop passed down from the parent? Controlled Components only refers to a stateful component whereas I have a stateless component. Replacing checked with defaultChecked does not do anything.




Aucun commentaire:

Enregistrer un commentaire