Consider the following example:
const userRole = sessionStorage.getItem('role');
const { data, setData, type, setTableType } = useTable([]);
useEffect(() => {
const getData = async () => {
// fetch some data from API
const fetchedData = await axios('..');
if (userRole === 'admin') {
setData([...fetchedData, { orders: [] }]);
} else {
setData(fetchedData);
}
if (type === '1') {
setTableType('normal');
}
};
getData();
}, []);
I want to run this effect ONLY on mounting and that's it, I don't care whether the userRole or setData has changed or not!
So, Now my questions are:
- Why ESLint is complaining about
userRolebeing missing in the dependencies array? Its not even a state! - Why ESLint is
complaining about
setDatabeing missing in the dependencies array isn't it will always be the same reference or does it change? - How to achieve what I want without upsetting the linter? Or should I straight slap the good old
// eslint-disable-line react-hooks/exhaustive-depsline? Or this is absolutely barbaric? - Why this indicates a bug in my code? I want to run this effect only once with whatever is available initially.
EDIT:
Let me rephrase the question What If these variables change and I don't care about their new values? Especially when type is getting updated in different places in the same file. I just want to read the initial value and run useEffect once how to achieve that?
react-hooks/exhaustive-deps warning