我现在正在使用<NavLink>
进行路由
,现在如何将props
传递到另一个组件中的<NavLink>
?
我现在正在使用<NavLink>
进行路由
,现在如何将props
传递到另一个组件中的<NavLink>
?
Link
和 NavLink
来将数据传递给导航组件的方式。<Link to={{
pathname:'/home',
state: {name:'from home page'}
}}>
Go to Home
</Link>
或者使用 NavLink
<NavLink to={{
pathname:'/home',
state: {title:'from home page'}
}}>
Go to Home
</NavLink>
在导航的组件内,您可以像这样访问它。
如果是钩子函数
import {useLocation} from 'react-router-dom'
function App(){
let location = useLocation();
console.log(location)
return <h2>...</h2>
}
如果它是基于类的组件
console.log(this.props.location)
<NavLink
to={{
pathname:"/priview-data",
aboutProps:{
selectedidds:this.state.selectedIds
}
}}
exact
>Preview Question
</NavLink>
现在转到另一个组件
constructor(props) {
super(props);
console.log(props.location.aboutProps);
}
希望它有所帮助
谢谢
<Link
to={{
pathname: "/home",
userProps: {name: "This is a passed prop"},
}}>
Go to home
</Link>
要从另一个 组件
访问它
import { useLocation} from "react-router-dom"
function OtherComponent() {
let location = useLocation();
console.log(location.userProps);
}
<NavLink to="/onboarding/profile" state={{ from: "occupation" }}>
Next Step
</NavLink>
import { useLocation } from 'react-router-dom'
function Profile () {
let location = useLocation();
console.log(location.state.from);
return (
...
)
}
<NavLink
className={`myStyle`}
to={{pathname: `/mypath`}}
state={myMessage}
>
message
</NavLink>
import { useLocation } from "react-router-dom";
.../...
function toto(){
let location = useLocation();
console.log (location.state)