Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React <Redirect> after transition not working

I have the following component which has a redirection route after an animation is finished, like so:

Menus.jsx

class Menus extends Component{
  constructor (props) {
    super(props);
    this.state = { 
        select: 'espresso',      
        isLoading: false,
        redirect: false
    };

  gotoCoffee = () => {
    this.setState({isLoading:true})
    setTimeout(()=>{
      this.setState({isLoading:false,redirect:true})
    },5000)  //Replace this time with your animation time
  }

  renderCoffee = () => {
    if (this.state.redirect) {
      return (<Redirect to={`/coffee/${this.state.select}`} />)
    }
  }

  render(){
    return (
      <div>
        <h1 className="title is-1"><font color="#C86428">Menu</font></h1>
        <hr/><br/>
        <div>
           {this.state.isLoading && <Brewing />}
           {this.renderCoffee()}
          <div onClick={this.gotoCoffee} 
               style={{textDecoration:'underline',cursor:'pointer'}}>
              <strong><font color="#C86428">{this.state.coffees[0]}</font></strong></div>
        </div>
      </div>
    );       
  }
}

export default withRouter(Menus);

The animation called onCLick:

Brewing.jsx

import React, { Component } from 'react';
import { withRouter } from 'react-router-dom';
import './css/mug.css'

class Brewing extends Component {
    constructor (props) {
    super(props);
  };
    render() {
        return (
            <div>
              <div className="cup">
                <div className="coffee"></div>
              </div>
              <div className="smoke"></div>
            </div>
        );
    }
}

export default withRouter(Brewing);  

And here redirected route component:

Coffee.jsx

class Coffees extends Component{
  constructor (props) {
    super(props);
    this.state = {
        select:'',
        template:''
    };
  };
  componentDidMount() {
    if (this.props.isAuthenticated) {
      this.getCoffee();
    }
  };
  getCoffee(event) {
    //const {userId} = this.props
    const options = {
      url: `${process.env.REACT_APP_WEB_SERVICE_URL}/coffee/espresso`,
      method: 'get',
      headers: {
        'Content-Type': 'application/json',
        Authorization: `Bearer ${window.localStorage.authToken}`
      }
    };
    return axios(options)
    .then((res) => {
      console.log(res.data.data)
      this.setState({
        template: res.data.data[0].content
      })
    })    
    .catch((error) => { console.log(error); });
  };

    render(){
        var __html = this.state.template;
        var template = { __html: __html };

        return (
           <div id="parent">
           <h1 className="title is-1"><font color="#C86428">{this.state.select} playlist</font></h1>
            <hr/><br/>
            <div dangerouslySetInnerHTML={template}/>
          </div>
        );
    }
}

export default withRouter(Coffees);

but <Redirect> in Menus.jsx is not working....url changes at browser but nothing happens; only if I refresh the browser /coffee is sucessfully mounted.


What I actually need to happen:

  1. render Menu
  2. click on a link
  3. click renders an animation
  4. when animation is done, after 5 seconds,
  5. <Redirect> to /coffee

what am I missing?

like image 582
8-Bit Borges Avatar asked Apr 26 '26 01:04

8-Bit Borges


1 Answers

When you say url changes at browser but nothing happens; only if I refresh the browser /coffee is sucessfully mounted.

This might be the issue with your Routes.

When you redirect to path /coffee/${this.state.select}, you should have Route to handle this path.

<Route path="/coffee/:select?" render={() => ( <Coffees isAuthenticated={true}/> )}/>

Note: Be aware of adding exact prop to Route. When you add exact prop it means your path should match exactly with all the provided params.

like image 188
ravibagul91 Avatar answered Apr 28 '26 17:04

ravibagul91