Skip to content

checkbox does not update to checked state upon hydration and rerendering #29749

Answered by ijjk
godmar asked this question in Help
Discussion options

You must be logged in to vote

Hi, the default value of useState is not meant to be changed like that and should be updated via setChecked instead, moving the query value check inside of a useEffect works properly e.g.

import * as React from 'react';
import { useState } from 'react';
import { useRouter } from 'next/router';

export default function TestCheckbox() {
  const router = useRouter();
  const [checked, setChecked] = useState(false);
  const handleChange = (ev) => { setChecked(!checked); }
  
  React.useEffect(() => {
    if (router.query.a === "true") {
      setChecked(true)
    }
  }, [router.query.a])
  
  return (
    <div>
        <input type="checkbox" 
               checked={checked} 
               o…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@godmar
Comment options

Answer selected by godmar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
bug Issue was opened via the bug report template.
2 participants
Converted from issue

This discussion was converted from issue #29747 on October 08, 2021 15:42.