ReactJS - 使用重定向组件传递道具 [英] ReactJS - Pass props with Redirect component
本文介绍了ReactJS - 使用重定向组件传递道具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你应该如何通过 Redirect
组件传递 props 而不让它们暴露在 url 中?
How should you pass props with the Redirect
component without having them exposed in the url?
喜欢这个 react-router-dom
.
Like this <Redirect to="/order?id=123 />"
? I'm using react-router-dom
.
推荐答案
你应该首先在你的 App.js 中定义的 Route 中传递 props
You should first pass the props in Route where you have define in your App.js
<Route path="/test/new" render={(props) => <NewTestComp {...props}/>}/>
然后在你的第一个组件中
then in your first Component
<Redirect
to={{
pathname: "/test/new",
state: { property_id: property_id }
}}
/>
然后在重定向的 NewTestComp 中,您可以像这样在任何地方使用它
and then in your Redirected NewTestComp you can use it where ever you want like this
componentDidMount(props){
console.log("property_id",this.props.location.state.property_id);}
这篇关于ReactJS - 使用重定向组件传递道具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文