gpt4 book ai didi

javascript - 转换后 React 不起作用

转载 作者:行者123 更新时间:2023-12-02 06:26:51 24 4
gpt4 key购买 nike

我有以下组件,它在动画完成后具有重定向路由,如下所示:

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

动画名为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);

这里是重定向的路由组件:

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

但是<Redirect> Menus.jsx 不工作....url 在浏览器中更改但没有任何反应;仅当我刷新浏览器时 /coffee挂载成功。


What I actually need to happen:

  1. 渲染菜单
  2. 点击链接
  3. 点击渲染动画
  4. 动画完成后,5 秒后,
  5. <Redirect>/coffee

我错过了什么?

最佳答案

当您说 url 在浏览器中更改但没有任何反应时;仅当我刷新浏览器时,/coffee 才成功安装。

这可能是您的 Routes 的问题。

当您重定向到路径/coffee/${this.state.select}时,您应该有Route来处理这个路径。

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

注意:请注意将 exact 属性添加到 Route。当您添加 exact 属性时,这意味着您的路径应该与所有提供的参数完全匹配。

关于javascript - 转换后 React <Redirect> 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57532219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com