score:1
Accepted answer
your function, on the outside, should be a dependency of your useeffect
. to avoid that, just move it inside the hook. other than that, looks good to me. reading a cookie is a side-effect, so you're doing it the right way.
useeffect(() => {
const readcookie = () => {
let user = false;
if(user = cookies.get("islogged")){
history.push('/homepage');
setauthenticated(true);
}else{
history.push('/');
}
}
readcookie()
},[]);
if you wanted to leave readcookies
outside of the hook, you'll have to wrap it in a usecallback
hook and use that in your dependency, like so:
const readcookiescallback = usecallback(() => {/* your read cookies code */}, [])
useeffect(() => {
readcookiescallback()
}, [readcookiescallback])
for what you're doing, that's unnecessary. my first example is the way to go.
Source: stackoverflow.com
Related Query
- React + redux + axios + thunk, wait for interceptors.response refresh token
- React axios interceptor for refresh token
- React: Is it ok to check cookies for refresh token?
- How do I check for token expiration and logout user?
- How to check for broken images in React JS
- React native Refresh works but next call still uses the last token
- Cannot check expect(elm).not.toBeVisible() for semantic-ui react component
- Axios interceptor refresh token for multiple requests
- Options for token storage and refresh in SPAs
- React Native: Transform Error for Unexpected Token (
- Axios global interceptor for token refresh
- Using OAuth2 refresh token in React Redux app
- Babel 7 Unexpected token for React component
- How to get JWT cookies in our react application, how to check the user is login or not I am unable to find how to handle my react application session
- AWS amplify google sigin with react doesn't automatically refresh token after 1 hour?
- Best way to set timer in React/Redux for sending refresh auth token
- Keeping user logged in after refresh/using refresh token with Google OAuth2 in React app
- React Router v4 login check after hitting Refresh on browser
- How to add refresh token function in react js redux
- How to keep multiple data for check boxes in react using firebase cloud firestore?
- How to check object in React for null or empty or undefined?
- Best way to check if there is already a token in local storage using use effect in React Context
- Set-cookie not working for cross-site request / response in Dot net Core 3.1 & React setup same-site cookies and/or CORS issue
- Attempts to check for element not existing with React Testing Library always failing
- React JS: Check for comma within input value
- Using middleware to check for users session react redux
- Unexpected Token - Jest for existing React + Web-pack app
- React react-router-dom private route not working when auth token is asynchronously loaded from cookies
- How can I check a function for having arguments without checking their values, using jest & enzyme in react component?
- React HOC to check for authorized user (protected routes)
More Query from same tag
- Wait for clicking until the element is rendered
- Changing value to true onClick on object in react redux
- Make request to SOAP endpoint using axios
- React router: catch a login-success url with a token being passed then redirect to homepage
- React Apollo Query keeps making network requests indefinitely with pollInterval
- Webpack error " ValidationError: Invalid parser object"
- Only trigger UseEffect() in Material UI Dialog when it is in opened
- Select one only one row in ant-design table component
- Getting error 405 when sending request on express route in next js
- What's the difference between useState and useImmer?
- Disabling dragndrop from baseweb DnD list
- Why is my original state mutated when filtering with react redux
- React component doesn't change parent's state
- Changing a react state variable according to drop down of select tag in jsx
- Unhandled Rejection (FirebaseError): Function DocumentReference.update() called with invalid data. Unsupported field value: a custom object
- Export to PDF by using Rest API(send to file) in my react project is getting blank PDF
- Error: C2008: Requirement had no matching files (ExtReactModels.model.Item) in ExtReact
- Redux Toolkit - Setting initial state using RTK Query
- React passing props from children to parent issue
- How to put a default value in a React component with redux?
- Logout Google Account with React Apps
- React: Module parse failed: Unexpected token
- Way to add checkbox checked event to cross out in list
- Checkbox onChange not fired
- The arrows does not showing in react slick with 4 and more photos
- npm install ->Failed at the node-sass@4.5.0 postinstall script
- AntD | Input.Search - Input disabled but keep the search button enabled
- TypeError: data.map is not a function, even though the data is an array (i think)
- Warning: Failed value type: The value `isRequired` is marked as required in `Select`, but its value is `undefined`
- How to pass properties from parent to child in Redirect