score:1
Accepted answer
the redirection is made even before the call to the api is made. useeffect
is called after the component has rendered. a solution would be to add an intermediary value, like null
or you let it undefined, it's just a way to know whether the call to the api has already been made or not.
const [permissions, setpermissions] = usestate(null);
// ...
if (permissions === null) return null
// ...
{permissions ? <route {...rest} /> : <redirect to="/" />}
Source: stackoverflow.com
Related Query
- Misunderstand using useState react hook - don't work properly
- React useState hook event handler using initial state
- prevent rerender for every setstate using react usestate hook
- How does React useState hook work with mutable objects
- React Hook does not work properly on the first render in gatsby production mode
- How does React useState and useCallback hook work when useCallback lacks dependencies
- Adding a key-value pair to an object inside loop using React useState hook
- React js using useState Hook
- How to change/add value of a certain row of an array using useState Hook in React
- Reading component state just after setting when using useState hook in react
- React component rendering twice when using useState hook
- Render Once for Multiple set States using useState hook in React
- The state variable returned after using useState react hook shows .map is not a function
- Why does useEffect React Hook not work properly with dependency?
- Using a function inside .map() in React , does not work properly
- Using prevState Callback function in UseState react Hook
- Why doesn't spread attributes work with React useState hook
- React replacing useState with useReducer while also using a custom hook
- Having issues using Pick utility type with React useState hook
- Button 'more' in a popup box doesn’t work right with React hook useState
- How To Set Display Style Property Using React useState Hook
- Browser tab freezes after using useState hook in React
- Invalid Hook Call Error when using react useState hook inside firebase app
- How to properly handle dynamic forms using react hooks useState
- React won't update state after using useState hook update function
- React - using a callback function with useState Hook
- React - Updating previous array with new values using useState hook
- React state not updated correctly when using useState hook
- React - infinite loop using get request when using useState hook
- Using array as react hook useState
More Query from same tag
- Async Meteor.call inside React components
- React Hook "useState" is called in function "setResults" which is neither a React function component or a custom React Hook function
- How can I mock FileReader with jest?
- Delay import in SystemJS
- React Component not Rendering New Data from Redux Store
- Masking in React-Pixi inlet/react-pixi
- React: .map is not a function
- reactjs onchange - conditional element rendering
- ReactJS display data from Wordpress API
- NextJS: How to correctly add firebase analytics to NextJS app
- Rendering json data with axios.get
- How to Customize Range Slider?
- Improvements to Recently Viewed Component in React
- What is throwing this error when building webpack application?
- auto call react this.function() in a onClick JSX event without getting an error
- window.matchMedia is not a function
- JSX - Syntax for creating a dynamic link when trying to load images from a database stored in state array
- Using list item key to pass data to another component
- How "history" object is getting evaluated in React
- how cache react prop to ref?
- Keeping redux and react-router in sync
- npm compiling warning (Unexpected template string expression no-template-curly-in-string)
- Pivotting a material UI dialog in the center of the screen when it's height may vary
- Gatsby: import components into a mdx file that is outside the src folder
- How to prevent extra hook calls on redux state change?
- Why does docker nginx container not render react Route?
- React useCallback function runs in infinite loop
- Call a function from a class in another file
- Redux reducers: map or not map on editing
- Email Input displaying incorrectly in Safari