score:1
why the token
isn't null
you can only store a string in localstorage
, so when you set it to null
and check later if the value is null
, the answer is no because localstorage
is now equal to the string "null"
, not null
.
what you can do instead is removing the token altogether :
localstorage.removeitem("token");
then simply check if token
exists :
let islogged;
if (token) {
islogged = "logout";
} else {
islogged = "log in";
}
return islogged;
you can also shorten your code with the following :
return token ? 'logout' : 'log in'
moreover
you are using dispatch
, which is a function for updating the store
by passing in an action object, however dispatch(logout());
, dispatch(handleopen());
and dispatch(handleclose());
are not passing in an object, these functions don't return anything. meaning you might (and should) run into errors because of this, since that's an improper use of the dispatch
function.
Source: stackoverflow.com
Related Query
- Why logout navlink doesn't work with localStorage?
- Why doesnt my react if statement work with "or"
- Why does Async Await work with React setState?
- Why formData does not work with multiple files?
- Why does TypeScript assertion of object literal `{a}` work with interface `{a, b}` but not `{a?, b}`
- why filtering state array with react hooks doesn't work but filtering original array does
- GET request works with Postman but why doesn't it work with ReactJS fetch?
- Why React Router NavLink prop exact (v5) vs. end (v6) lead to different result with trailing slash in url
- Why does select() not work on Safari with ReactJS?
- Typescript: Why doesn't Material-UI "withStyles()" work with an explicit constructor?
- Why readonly keyword doesn't work with destructuring
- Socket.IO in React app bundled with Vite doesnt work (When bundled with Webpack it does)
- Why doesn't MobX v6.x work as expected in React with Typescript?
- Why number validate rule doesnt work in antd
- Why does custom Button component using MUI Button not work on hover with Tooltip?
- Why Twitter Typeahead doesn't work with React js?
- Why credit card autofill doesn't work when production build but it works with npm start in my react application?
- Why the input doesnt work when generated by React
- Why wouldn't React Router work with <Switch>?
- Javascript Optional chaining doesnt work with template literals
- If I create custom Button with material-ui ButtonGroup doesnt work
- Why ES6 ComputedPropertyName does not work with this react js code?
- Why does destructoring works with events, and wont work without it
- Why this doesn't work with recompose and ramda?
- With Styled Components why do pseudo classes seem to still work without ampersands?
- Why does useEffect React Hook not work properly with dependency?
- Why doesn't enzymes find() with component props work the same way when using full rendering or shallow rendering?
- React router NavLink activeClassName does not work with redux and styled components
- Why doesn't spread attributes work with React useState hook
- Ant design Tree defaultExpandAll doesnt work with button click for react
More Query from same tag
- React functional component - how to count instances?
- How to include a page in React?
- How to have a edit icon show on a individual row item when it is hovered
- Counter part of AngularJS attribute directives in React
- overflow-hidden in reactstrap
- Reactjs: How to prevent child from modifying props?
- React Hooks Vs React-redux
- How to apply nested styles to my react form component,only main styles are appending not nested ones
- React JS & Redux: Rendering Multiple Components within the Provider Throws an Undefined Error
- How do I successfully deploy a react app to Azure
- passing values from component to redux store
- How to secure edit form route?
- Pass component via props in react that matches type
- Unable to play a different song using setState method in ReactJs
- How to move SemanticUIReact Icon left of text element in sub-menu?
- Use Navigate function not working (React)
- Match an array of values in url in React Router
- Pressing Tab to active Enter key
- With react-router is ite possible to access context from a Route's onEnter prop?
- (concurrently not found) trying to deploy a PERN app to Heroku
- `TypeError: Cannot read property 'map' of undefined` in React in functional component
- Map() returning duplicate component
- How to customise React-Day-Picker input?
- How we can make a div placed inside a link tag to be non-navigated
- In React get SyntaxError : Unexpected token
- update the state in React JS array using useSatate
- docker-compose cant run react without node_modules already existing
- What part of my CSS is stopping the media queries from working? React-app
- Building search filter using ReactJs, and Node.js
- Using Morris.js with reactjs