现在我正在使用重定向到新页面,但我不知道如何将数据传递到新页面。这就是我正在做的事情:-
class ShopsCatOptions extends React.Component{
constructor(props){
super(props);
this.state={
shopd: this.props.shop,
redirect:false
};
}
render() {
if (this.state.redirect) {
return <Redirect push to="./shopdetail" data={this.state.redirect}/>;
}
return(
<div class="expndinnerm" onClick={this.handleOnClick}>
{
this.state.shopd
}
</div>
)
}
}
export default ShopsCatOptions
正确的做法是什么?
最佳答案
我为实现这一目标所做的就是使用历史 Prop ,
this.props.history.push({
pathname:"/shopdetail",
state:{
key:"value"
}
});
在 ShopDetail 组件中,您可以访问该对象,例如,
this.props.location.state.key
编辑:要获取 Prop 中的历史记录,您的主要组件应该具有,
import { BrowserRouter, Route } from 'react-router-dom';
...
<BrowserRouter>
<Switch>
<Route path="/home" component={Home} />
<Route path="/user" component={User} />
<Route path="*" component={page404} />
</Switch>
</BrowserRouter>
关于reactjs - 如何在 React 中将数据传递到 <redirect> 中的页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50653515/