I have declared a variable like
const [listRealm, setListRealm] = useState([
{
value: null,
label: 'select one',
},
]);
const findList= async () => {
await axios.get(`${apiUrl}`).then(resp => {
resp.data.map(res => {
setListRealm(listRealm => [...listRealm, { value: res.id, label: res.name}]); // error here
});
});
};
useEffect(() => {
findList();
}, []);
Now my problem is that I receive error:
'listRealm' is already declared in the upper scope @typescript-eslint/no-shadow
I don't understand how I can change name of variable, considering that i'm setting this variable.
How can I do? thank you
The name you use in the setListRealm
callback can be anything you want, so you can just make it previouslistRealm
or something:
setListRealm(previousListRealm => [...previousListRealm, { value: res.id, label: res.name}]);
It doesn't have to match the name of the constant you've put the value from useState
in.
setListRealm(listRealm <-- You can name it here.