通过页面之间的数据反应

发布于 2025-02-06 18:31:25 字数 527 浏览 1 评论 0原文

我有eventData,我需要从第A页传递到B。这是我的代码:

function eCE(eventData) {

    history.push({
      pathname: '/ece',
      state: eventData
   })

})

此功能在按钮单击上触发,因此它传递eventData通过该功能。现在,我需要将eventData发送到下一页,我可以通过调用/ece来获取。但是,我试图在另一页上抓住状态这样的:

const { state } = this.props.location
console.log(state)

它不起作用,而我得到的错误是:

TypeError: undefined has no properties

不确定在这里做什么。感谢帮助!

I have eventData which I need to pass from page A to B. Here's my code:

function eCE(eventData) {

    history.push({
      pathname: '/ece',
      state: eventData
   })

})

This function gets triggered on button click, so it passed eventData through to the function. Now I need to send eventData through to the next page, in which I would get to by calling /ece. However, I am trying to grab the state on the other page like so:

const { state } = this.props.location
console.log(state)

it isn't working, and the error I get is:

TypeError: undefined has no properties

not to sure what to do here. appreciate the help !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骷髅 2025-02-13 18:31:25

在第1页:

import { useHistory } from "react-router-dom";

function page1(){   
  const history = useHistory();

  const goToPage2 = () => {
    history.push("/page2", { age: 18 });
  }
  return <button onClick={goToPage2}>Go To Page2</button>
}

在第2页中:

import { useLocation } from "react-router-dom";

function page2(){ 
  const location = useLocation();
  return <div><p>{location.state.age}</p></div>
}

In page 1:

import { useHistory } from "react-router-dom";

function page1(){   
  const history = useHistory();

  const goToPage2 = () => {
    history.push("/page2", { age: 18 });
  }
  return <button onClick={goToPage2}>Go To Page2</button>
}

In page 2:

import { useLocation } from "react-router-dom";

function page2(){ 
  const location = useLocation();
  return <div><p>{location.state.age}</p></div>
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文