+1 vote
by
When I enter the page, I want to make a fetch request if constToken variable has a value. But for some reason it works even when constToken == false.
React.useEffect(() => {
const getToken = localStorage.getItem('token')
if(getToken !== '') {
changeValue('token', getToken)
fetch('http://localhost:1717/profile', {
method: 'GET',
headers: { 'X-Auth': `${getToken}` },
})
.then(function (response) {
return response.json()
})
.then(function ({data}) {
setId(data.id)
changeValue('profile', data)
})
}

}, [changeValue, changeProfile, login])
by
false !== '' // true

1 Answer

0 votes
by
 
Best answer
ВlocalStorage only strings are stored. If you do thislocalStorage.setItem('token', false) , then atlocalStorage.getItem('token') you don't get'' , and the result of bringingfalse to the line, i.e.'false'.
Howeverfalse !== '' - is also true, because!== comparison without type conversion.
...