加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react-router-dom 通过Link传值的坑人表现!

发布时间:2020-12-15 06:42:57 所属栏目:百科 来源:网络整理
导读:react-router-dom 通过Link传值的坑人表现! link 可以通过以下方式传递到下一个路由: Link to={{ pathname: '/courses',search: '?sort=name',hash: '#the-hash',state: { fromDashboard: true }}}/ 在下一个页面可以这样取到值 (通过props.location.state

react-router-dom 通过Link传值的坑人表现!

link 可以通过以下方式传递到下一个路由:

<Link to={{
  pathname: '/courses',search: '?sort=name',hash: '#the-hash',state: { fromDashboard: true }
}}/>


在下一个页面可以这样取到值 (通过props.location.state 取值)

class CompName extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      param:props.location.state,search:props.location.search,}; 
 console.log("state-----:",this.props) }
}

------------------------但是-----------------------------

如果在Link里面加个 target="_blank",这样的话,state里面的值就是undefined
<Link to={{
  pathname: '/courses',state: { fromDashboard: true }
  target="_blank"
}}/>
不明白这是个什么设计,猜想可能是: 在新窗口打开的话,已经脱离了当前的页面state,(相当于window.open新窗口了吗?),这样就已经不是单页面应用了??? 所以React如果想要在新窗口打开的页面之间传递参数,还是老实的用search/hash这些方法吧 !

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读