window.getComputedStyle(…).getPropertyValue(‘width’) is returning wrong value on chorome

I’m trying to determin the width of a 1 char element defined simply as :

<div ref={refBBFS} style={{width:"1ch",fontFamily:'some monospace font'}}>X</div>

[ using nextjs 13.1.1 , reactjs 18.2.0, chrome 80.0.3987.132]

window.getComputedStyle(…).getPropertyValue(‘width’) seems to return the wrong value on chrome. ( my code works on firefox ).

its currently placed in a useEffect defined in my root element , and its executed for every render (i.e. using “,[]” ).

the value does not match what is shown in the devtools , in both the graphical view of the element, and in its computed “width” variable.

specifically, I’m getting a value of 8px for font size 16 (medium) , 6px for size 12 and so on, while chrome dev tools shows a value of 9.63281 ( and firefox shows 10.1833).
It seems to me like i’m getting a default/initial value, before some calculation has occured.

I have also executed the call to getComputedStyle(…).getPropertyValue on every window.resize event,
and when its executed on a resize event I get the expected value.
(all values derived on the resize events are exactly the same, since the resize event does not change the computed value of my element. but they differ from the initial value received after page load).

I can also share that when I change my code, and trigger online rebuild of nextjs,
the folowing render/execution gets the correct value.

but otherwise, simple serving of the page and refreshing (using f5) return the wrong value.

its as if i’m executing the cal to window.getComputedStyle(…).getPropertyValue(‘width’) to early. but I don’t understand how and why this behaves differently on firefox.

I tried working around the problem by skipping the first render, and by explicitly triggering the resize event on the first render, but the error persisted.

example code

export default function MyApp({ Component, pageProps }: AppPropsWithLayout)
{
    const refBBFS = useRef(null);
    useEffect(() =>
    {
        const BaseWidthPx =  window.getComputedStyle(refBBFS.current).getPropertyValue('width');
        const BaseFSPx    =  window.getComputedStyle(refBBFS.current).getPropertyValue('font-size');
        console.log('my app effect',BaseWidthPx,BaseFSPx);


        // add resize event handler
        function HandleResolutionChange()
       {

         console.log('** executing resolution change ** ',window.getComputedStyle(refBBFS.current).getPropertyValue('width'),window.getComputedStyle(refBBFS.current).getPropertyValue('font-size'));
        }
       window.addEventListener('resize', HandleResolutionChange);
       HandleResolutionChange();

       return () =>
       {
         window.removeEventListener('resize', HandleResolutionChange);

       }


    }, []);

return (<>
     <div ref={refBBFS} style={{width:"1ch",fontFamily:'...'}}>X</div>
</>);
}

2

It seems that the error does not reproduce on edge, or newer versions of chrome, so It may be specific to some older versions of chrome and thus not a major issue at the moment

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