代码之家  ›  专栏  ›  技术社区  ›  8-Bit Borges

过渡后反应<Redirect>不起作用

  •  0
  • 8-Bit Borges  · 技术社区  · 5 年前

    菜单.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);
    

    onCLick :

    酿造.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);  
    

    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);
    

    但是 <Redirect> 在里面 菜单.jsx 不工作…url在浏览器中更改,但什么也没有发生;只要我刷新浏览器 /coffee


    我真正需要的是:

    1. “渲染”菜单
    2. 单击链接
    3. <重定向> /咖啡

    我错过了什么?

    2 回复  |  直到 5 年前
        1
  •  6
  •   ravibagul91    5 年前

    当你说 /coffee

    这可能是你的问题所在 Routes .

    当你 redirect 到路径 /coffee/${this.state.select} Route 来处理这条路。

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

    注意添加 exact 道具 路线 . 当你添加 准确的 prop表示您的路径应该与提供的所有参数完全匹配。

        2
  •  1
  •   Diamond shyke    5 年前

    你需要打电话 getCoffee 功能也在 componentDidUpdate 功能。

      componentDidMount() {
        if (this.props.isAuthenticated) {
          this.getCoffee();
        }
      };
      componentDidUpdate() {
        if (this.props.isAuthenticated) {
          this.getCoffee();
        }
      };
    
        3
  •  1
  •   Japsz    5 年前

    你的重定向应该在 render()

    render(){
        if(this.state.redirect) {
            return(<Redirect to={`/coffee/${this.state.select}`} />)
        } else {
            return ( 
                <div>
                   ...your component
                </div> ); 
        }
    }
    

    注意这样你就不需要 renderCoffee() 功能。

        4
  •  1
  •   Morlo Mbakop    5 年前

    似乎你的菜单组件没有右括号。

    ...
    
    class Menus extends Component{
      constructor (props) {
        super(props);
        this.state = { 
            select: 'espresso',      
            isLoading: false,
            redirect: false
        };
      } // did you miss this?
    
    gotoCoffee = () => {
    
    ...