Checkbox cannot change state when updated from onBlur event

I have a component (selector) which contains a checkbox and another div (selectorPane).

The checkbox state controls the selectorPane visibility (by assigning/not-assigning the ‘visible’ css class).

This works fine, the problem comes when i try to make selectorPane invisible (set checkbox.checked = false) on selectorPane.onBlur() event… in other words, close selectorPane when clicking outside.

This is the working code (without the ‘click outside to close‘ functionality).

import { useEffect, useRef, useState } from "react"

import "./Selector.css"

export default function Selector() {
  //reference selector-pane for programatic focus
  const selectorPaneRef = useRef<HTMLDivElement | null>(null);

  const [ selectorPaneVisible, setSelectorPaneVisible ] = useState(false);

  const handleCheckboxStateChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const checkbox = e.target as HTMLInputElement;
    setSelectorPaneVisible(checkbox.checked);
  }

  //autofocus selector-pane when rendering with selectorPaneVisible = true
  useEffect(() => {
    if (selectorPaneVisible && !!selectorPaneRef.current) {
      selectorPaneRef.current.focus();
    }
  }, [selectorPaneVisible])

  return (
    <div className="selector">
      <input
        type="checkbox"
        checked={selectorPaneVisible}
        onChange={handleCheckboxStateChange}
      />
      <div
        className={`selector-pane ${selectorPaneVisible ? "visible" : ""}`}
        tabIndex={-1}
      >
        content content content content
      </div>
    </div>
  )
}

note: autofocus functionality is not used here, but is kept to demonstrate it doesn’t add a bug

Here is the NOT-WORKING example:

It has added the onBlur (function: handleSelectorPaneBlur) event for the selectorPane div which performs a setSelectorPaneVisible(false)

import { useEffect, useRef, useState } from "react";

import "./Selector.css";

export default function Selector() {
  //reference selector-pane for programatic focus
  const selectorPaneRef = useRef<HTMLDivElement | null>(null);

  const [selectorPaneVisible, setSelectorPaneVisible] = useState(false);

  const handleCheckboxStateChange = (
    e: React.ChangeEvent<HTMLInputElement>
  ) => {
    const checkbox = e.target as HTMLInputElement;
    setSelectorPaneVisible(checkbox.checked);
  };

  const handleSelectorPaneBlur = () => {
    setSelectorPaneVisible(false);
  };

  //autofocus selector-pane when render with selectorPaneVisible = true
  useEffect(() => {
    if (selectorPaneVisible && !!selectorPaneRef.current) {
      selectorPaneRef.current.focus();
    }
  }, [selectorPaneVisible]);

  return (
    <div className="selector">
      <input
        type="checkbox"
        checked={selectorPaneVisible}
        onChange={handleCheckboxStateChange}
      />
      <div
        className={`selector-pane ${selectorPaneVisible ? "visible" : ""}`}
        tabIndex={-1}
        ref={selectorPaneRef}
        onBlur={handleSelectorPaneBlur}
      >
        content content content content
      </div>
    </div>
  );
}

By default the checkbox is unchecked, so the selectorPane is invisible

  • on first click is checks correctly, shows the div, and it autofocuses
  • if i click outside the pane (anywhere), it unchecks and hides de pane well
  • the problem is that i cant hide the pane by clicking on the checkbox itself (it flashes unchecked & checked again instantly)

This is the CSS:

.selector > div {
  background-color: green;
}
.selector > div:focus {
  background-color: red;
}

.selector > div {
  visibility: hidden;
}
.selector > div.visible {
  visibility: visible;
}

I tested creating a ref to the checkbox element to uncheck it by firing a programatic click event

const checkboxRef = useRef<HTMLInputElement | null>(null)

<input 
  type="checkbox" 
  checked={selectorPaneVisible} 
  onChange={handleCheckboxStateChange} 
  ref={checkboxRef} 
/>
const handleSelectorPaneBlur = () => {
  if (checkboxRef.current) {
    checkboxRef.current.click();
  }
}

but the result is the same (unchecks & checks again in a flash)

  • The handleCheckboxStateChange function is fired only ONCE when I click the checkbox to uncheck it

New contributor

timo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

The issue is with the onBlur callback (actually that’s how it’s supposed to work).

When the checkbox is checked, it sets the focus to the selectorPane. If you click the checkbox again to toggle visibility, the focus shifts from the selectorPane to the checkbox, causing the onBlur event of the selectorPane to fire prematurely.

This hides the selectorPane before the checkbox click event can complete, leading to flickering as the state is reset.

To verify this issue, you can add a delay to the state update inside the onBlur callback. For example:

const handleSelectorPaneBlur = e => {
  setTimeout(() => {
    setSelectorPaneVisible(false);
  }, 500);
};

Adding a timeout to the state update in the onBlur callback delays hiding the selectorPane, allowing the checkbox click event to complete properly.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật