'UseRole for checking Role in react js
I have created a useUserRole custom hook Is it the right way to use
import {Redirect, useHistory} from 'react-router-dom' import {useAuth} from './useAuth'
export const useUserRole = (role) => {
const history = useHistory();
console.log('role' , role);
const {session} = useAuth()
const role_id = session?.user?.role_id
console.log(role_id);
useEffect(() => {
if (role_id !== role) {
history.push('/error')
}
}, []);
In my component I used custom hook like this If user is Supplier than only component will render else throws error.
const ViewRoomProperty = () => {
Role = {
Admin: 'Admin',
Supplier: 'Supplier',
}
const role = useUserRole(Role.Supplier);
return (
<>
<div className='gallery'>
<div>
<ViewRoom data={data} />
</div>
</div>
</>
)
}
export default ViewRoomProperty
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
