score:7
when you call logout
at onclick
you are calling hooks from your function logout
outside of a component body, what's is not allowed.
you can abstract your logout logic to a custom hook where it returns the logoutuser
function:
export default function uselogout() {
const history = usehistory();
// we don't useeffect here, we are only interested in function logoutuser
const logoutuser = () => {
const response = axiosinstance.post('user/logout/blacklist/', {
refresh_token: localstorage.getitem('refresh_token'),
});
localstorage.removeitem('access_token');
localstorage.removeitem('refresh_token');
axiosinstance.defaults.headers['authorization'] = null;
history.push('/login');
}
return { logoutuser }
};
then you consume your custom hook at your loginuser
component body, extracting the logoutuser
method:
function loginuser()
{
const user = useselector(selectuser);
// use hook at component body extracting logoutuser method
const { logoutuser } = uselogout();
const dispatch = usedispatch();
const handlelogout = (e) =>
{
if(e) e.preventdefault();
dispatch(logout());
};
const userloggedinalert = () =>
{
return <container>
<span>
you are already logged in!
</span>
<button
component={navlink}
variant="contained"
color="primary"
to="/logout"
// here now you can safely logout user since no hooks are being called
onclick={function(){ handlelogout(); logoutuser()}}
>
do you want to logout?
</button>
</container>
};
return (
<container>
{user ? <userloggedinalert/> : <signin/> }
</container>
);
}
export default loginuser;
you can take a step ahead and simplify your logout
component:
export default function logout() {
const { logoutuser } = uselogout();
useeffect(() => {
logoutuser()
});
return <div>logout</div>;
};
edit
for simplicity at your logout.js do as:
export const uselogout = () => {
const history = usehistory();
// we don't useeffect here, we are only interested in function logoutuser
const logoutuser = () => {
const response = axiosinstance.post('user/logout/blacklist/', {
refresh_token: localstorage.getitem('refresh_token'),
});
localstorage.removeitem('access_token');
localstorage.removeitem('refresh_token');
axiosinstance.defaults.headers['authorization'] = null;
history.push('/login');
}
return { logoutuser }
};
export default function logout() {
const { logoutuser } = uselogout();
useeffect(() => {
logoutuser()
});
return <div>logout</div>;
};
at your your login you import the hook with curly brackets:
import { uselogout } from '../auth/logout';
and at your index.js without the curly brackets you import the component:
import logout from './components/auth/logout';
const routing = (
<router>
<route path="/logout" component={logout} />
</router>
);
score:1
components are basically function references, and you are invoking them, so instead of doing signin()
, try <signin />
edit: and make userloggedinalert
start with upper case, then all together:
{user ? <userloggedinalert /> : <signin />}
Source: stackoverflow.com
Related Query
- React Redux-Toolkit: Invalid hook call - Hooks can only be called inside of the body of a function component
- React call from class to functional component. Error: Invalid hook call. Hooks can only be called inside of the body of a function component
- React + Antd + Rollup Component Library "Error: Invalid hook call. Hooks can only be called inside of the body of a function component"
- React 17.0.1: Invalid hook call. Hooks can only be called inside of the body of a function component
- Invalid hook call. Hooks can only be called inside of the body of a function component when i call useQuery in useEffect
- React Hooks Mobx: invalid hook call. Hooks can only be called inside of the body of a function component
- React - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. What is wrong my syntax?
- Getting this error "Invalid hook call. Hooks can only be called inside of the body of a function component." inside a react functional component?
- Error: Invalid hook call. Hooks can only be called inside of the body of a function component. by Routing in react
- error: invalid hook call Hooks can only be called inside of the body of a function component
- React Hooks: Instantiating state hooks on validation Error: Invalid hook call. Hooks can only be called inside of the body of a function component
- React Hooks + Mobx => Invalid hook call. Hooks can only be called inside of the body of a function component
- React - Invalid hook call. Hooks can only be called inside of the body of a function component
- Invalid hook call. Hooks can only be called inside of the body of a function component in react 18
- React Router: Invalid hook call. Hooks can only be called inside of the body of a function component
- React and Easybase - Invalid hook call. Hooks can only be called inside of the body of a function component
- How to avoid error : Invalid hook call. Hooks can only be called inside of the body of a function component?
- Error: Invalid hook call. Hooks can only be called inside of the body of a function component. - React
- React hook Error: Invalid hook call. Hooks can only be called inside of the body of a function component
- Getting Invalid hook call Hooks can only be called inside of the body of a function component with SharePoint Framework Template
- Invalid hook call. Hooks can only be called inside of the body of a function component error
- getting error :Invalid hook call. Hooks can only be called inside of the body of a function in react typescript
- Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component when using Create React App and Formik
- I am getting the error Invalid hook call. Hooks can only be called inside of the body of a function component
- React 17 - Adding Evergreen UI Pane Gives an Error: Invalid hook call. Hooks can only be called inside of the body of a function component
- Error: Invalid hook call. Hooks can only be called inside of the body of a function component. please help me to solve this error
- Server Error Error: Invalid hook call. Hooks can only be called inside of the body of a function component in _app.js
- React - Error: Invalid hook call. Hooks can only be called inside of the body of a function component
- React useEffect Invalid hook call. Hooks can only be called inside of the body of a function component
- Invalid hook call. Hooks can only be called inside of the body of a function component
More Query from same tag
- Correct Typescript types for Firebase in a React app
- Multi step form using react hooks
- Using pouchdb-load with a local file
- Why the typing is different when specifying props directly on component, or inside another prop?
- Create react app Error: Cannot find module './locale'
- How to make sure map is loaded with google-map-react
- React make table clickable and edit details
- How do I sort a list of objects based on key with conditional access in Javascript (and Flow)?
- Reactjs/SemanticUi add if condition in map
- Assigining value to the Select tag's value property in React js
- WebpackError: Cannot read property 'pathname' of undefined, with gatsby and netlify
- React - how to filter items in dropdown
- How to handle the render of a large list of items in react?
- PropTypes shape get marked as required
- React - React Router Dom, hashbang routing solution
- es lint '' is assigned a value but never used' eventhough i have used it
- can't repeat an hexadecimal html entity in react jsx value props
- React: open link in a new tab
- 'npm run build' failed in AWS CodeBuild but succeeded locally
- How do you do a "day" picker in Ant Design?
- Is it possible to do custom imports with Webpack like it is with Browserify?
- Can't get Typescript to understand the css prop (emotion) on Material-UI components
- Problem with rewriting toTS React HOC which limits visibility
- ReactJS: How to test a component function?
- Dealing with refs in function component without forwardRef
- I am using gh-pages to deploy a github page but I'm getting an error now when it was working fine before
- How to count selected checkboxes in React functional component?
- REACTJS Add a condition to .map to check if the element is contained in an array
- JSX error: Property does not exist on type 'JSX.IntrinsicElements
- Lazy load components requiring authentication with react-redux