Home > Mobile >  Redirect in React router dom not redirecting to the given path
Redirect in React router dom not redirecting to the given path

Time:10-02

I have a make payment function which is called on a button click and if the user is not signed in i want to redirect that user to the signin component . This is my code of the function.

  const PaymentMethod = ()=>{
        
      if(!isAuthenticated()) 
      {
           toast.error('Please signin to continue');
           history.push('/signin') //works properly
          // return <Redirect to = '/signin' /> // not working properly
           
      }
      
   }

// isAuthenticated is a boolean function that look whether the user is signed in or not 

CodePudding user response:

Try

return <Redirect to = '/signin' /> 

Because you have to render the Redirect to make an effect

CodePudding user response:

It looks like <Redirect to = '/signin' /> is a component and not a JS that can be called. Components need to be rendered in order for their JS to run.

If you for some reason want to specifically use redirect component you will have to put it into DOM on click.

  • Related