DEV Community

Cover image for React, Vue and Svelte: Comparing Checkbox Binding
Clément Creusat
Clément Creusat

Posted on • Updated on

React, Vue and Svelte: Comparing Checkbox Binding

Checkbox Binding in...

You should not fear checkbox binding in any of these frameworks. At least, one checkbox is easy to handle :)
But as we delve deeper into Form Binding, you'll see that Vue and Svelte offer something fluid.

Check it out 🚀

React

Live Example

const [checked, setChecked] = useState<boolean>(false);
const handleCheckbox = () => setChecked(!checked);

<section>
  <h2>Checkbox</h2>
  <input
    type="checkbox"
    id="checkbox"
    checked={checked}
    onChange={handleCheckbox}
  />
  <label for="checkbox">Checked: {checked.toString()}</label>
</section>
Enter fullscreen mode Exit fullscreen mode

Vue

Live Example

const checked: Ref<boolean> = ref(false);

<section>
  <h2>Checkbox</h2>
  <input type="checkbox" id="checkbox" v-model="checked" />
  <label htmlFor="checkbox">Checked: {checked.toString()}</label>
</section>
Enter fullscreen mode Exit fullscreen mode

Svelte

Live Example

let checked: boolean = false;

<section>
  <h2>Checkbox</h2>
  <input type="checkbox" id="checkbox" bind:checked={checked} />
  <label for="checkbox">Checked: {checked}</label>
</section>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)