繁体   English   中英

React.JS 通过 state 链接,props.location.state 未定义

[英]React.JS pass state to link, props.location.state is undefined

我将 state 传递给Link组件并尝试使用props.location.satate 不幸的是,state 未定义。 我尝试使用pathname和 state 从to属性发送state ,但仍未定义。

<Route exact path="/private/orders" name="Orders" component={PrivateOrders} />

私人订单

import React from 'react'

const PrivateOrders = (props) => {
    console.log(props)
    return (
        <div>
            <h1>Orders</h1>
        </div>
    )
}

export default PrivateOrders

关联

<Link  
   to="private/orders"
   state={{
      x: "y",
      y: "y",
      z: "y",
      i: "y",
   >
      <CButton color="primary">Order Now</CButton>
</Link>

结果

location: {pathname: '/private/orders', search: '', hash: '', state: undefined, key: 'lt9bpr'}

像这样在 Route 中传递 routeProps。

<Route exact path="/private/orders" name="Orders" component={(routeProps) => <PrivateOrders {...routeProps} />} />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM