gpt4 book ai didi

javascript - 在 React js 中重定向时将参数传递给新页面

转载 作者:行者123 更新时间:2023-12-05 01:29:36 25 4
gpt4 key购买 nike

当从当前页面定向时,我正在努力将参数传递给新页面

这是我当前的页面 SingleBox

function SingleBox(props){
let history = useHistory();
function moveToSinglePost() {
history.push({
pathname: "/single-post",
state: props
})
}
return(
// <></>
<Grid item md = {4} css = {GridCell}>
<div onClick = {moveToSinglePost}>
<Grid style = {singleBox}>
<img style = {singleImage} src = {props.BoxProps.val.image} />
<p style = {userDisplay}> {props.BoxProps.val.user}</p>
</Grid>
</div>
</Grid>
)
}

这是新页面 SinglePostView

function SinglePostView(props){
console.log("singlePostView", props);
return (
<>
<AppNavBar/>
<Grid>
<Grid item md = {6} style = {post}>
<Grid container style = {displayImage} >
<Grid item style = {singleBox}>
{/* <img src = "props.link"/> */}
</Grid>
</Grid>
</Grid>
</Grid>
</>
)
}

我在 SinglePostView 中打印的 props 与我想要的不一样(其中包含 val、key、..):

singlePostView 
{history: {…}, location: {…}, match: {…}, staticContext: undefined}
history: {length: 2, action: "PUSH", location: {…}, createHref: ƒ, push: ƒ, …}
location: {pathname: "/single-post", state: {…}, search: "", hash: "", key: "0hx5t4"}
match: {path: "/single-post", url: "/single-post", isExact: true, params: {…}}
staticContext: undefined
__proto__: Object

最佳答案

如果你像那样传递 props,它们可以在 react-router 的 location 对象中检索(在 SinglePostView 中)

const location = useLocation()
const { props } = location.state

关于javascript - 在 React js 中重定向时将参数传递给新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67638847/

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