I am trying to calculate some data based on my window size.
Each time we resize the window, I calculate the number of tabs I can put inside my tab bar.
I use the hook useWindowSize
from react-use
for this.
The problem is that the linter is complaining that:
React Hook useEffect has missing dependencies: 'navIndex', 'numberOfChunkedElement', 'tabs', and 'value'. Either include them or remove the dependency array.
The issue is that I can't put value
inside the dependency array because it will cause an infinite loop.
Here is my code:
const [value, setValue] = useState([0, 0]); // [selectedChunckedNavIndex, selectedIndex]
const [navIndex, setNavIndex] = useState(0);
const [numberOfChunkedElement, setNumberOfChunkedElement] = useState(0);
const [chunkedTabs, setChunkedTabs] = useState<TabProps[][]>([[]]);
/* [...] */
useEffect(() => {
// Each time the window is resized we calculate how many tabs wwe can render given the window width
const padding = 20; // The AppBar padding
const numberOfTabIcons = navIndex === 0 ? 1 : 2;
const wrapperWidth =
wrapper.current.offsetWidth - padding - numberOfTabIcons * 30;
const flattenIndex = value[0] * numberOfChunkedElement + value[1];
const newChunkedElementSize = Math.floor(wrapperWidth / 170);
setNumberOfChunkedElement(newChunkedElementSize);
setChunkedTabs(chunkArray([...tabs], newChunkedElementSize));
setValue([
Math.floor(flattenIndex / newChunkedElementSize),
flattenIndex % newChunkedElementSize,
]);
}, [width /* , navIndex, numberOfChunkedElement, tabs, value */]);
My question is How can I re-calculate some data and set them inside my component each time the width changes ?