I want to get windowsize of the browser and according to this condition I want to render something in the component. When I try to get clientWidth using useEffect, I get the value. However, I can not assign it the variable which I defined it in useState.
This is my code. and sometimes I'm getting the following error which I don't understand because I don't assign anything to constant variable. I try to assign variable of useState.
import React, { useState, useEffect } from 'react';
export default function App() {
const [isMobile, setIsMobile] = useState(false);
useEffect(() => {
if (document.documentElement.clientWidth <= 400) {
console.log(document.documentElement.clientWidth);
setIsMobile = true;
}
}, []);
return (
<div>
{(() => {
console.log(isMobile);
if (isMobile === true) {
return <div>This is Mobile</div>;
} else {
return <div>This is normal</div>;
}
})()}
</div>
);
}
And this is the written version in stackblitz.