选择带有信息的 div 时未定义

发布于 2025-01-18 04:30:53 字数 1143 浏览 0 评论 0 原文

我的一段代码有问题。我从 Flask 服务器获取,并在 React 中用 div 显示。我想选择 div 并将该信息传递到一个新的对象数组以返回到 Flask,但我一直未定义。

代码片段:

function PLCPage() {
  const [myjunk, setMyjunk] =  useState([]);
  const [devList, setDevList] = useState ([]);

  const Scan = () => {
    fetch('/api/home').then(response => {
      if(response.status === 200){
        return response.json()
      }
    })
    .then(data => setMyjunk(data))
    .then(error => console.log(error))
  }

  const Clear = () => {
    setMyjunk({})
  }

创建div:

{Object.keys(myjunk).map((key) =>{
  return ( 
    <div className='plc-container' key={key} onClick={ReadStuff}>
      <h1>ID:{myjunk[key]['name']}</h1> 
      <h1>IP:{myjunk[key]['IP']}</h1>                      
    </div>
  )

单击div,只是为了返回控制台日志,返回未定义。

const ReadStuff = () => {
  console.log(this.IP)
}

我最终想将 2 h1 标签中的数据返回到一个新对象(devList),但我什至无法将其获取到控制台日志。抱歉,如果这是基本的,但我已经被这个问题困扰了一个星期了。谢谢,

我已经尝试过this.IP、myjunk.IP、this、myjunk.IP。 myjunk['IP'].什么都没有返回。当我执行 myjunk.IP 时,我得到“无法从未定义中读取”

Having an issue with a piece of my code. I fetch from flask server, and display with div in React. I want to select the div and have that information pass to a new object array to return back to flask, but I keep getting undefined.

Code snippet:

function PLCPage() {
  const [myjunk, setMyjunk] =  useState([]);
  const [devList, setDevList] = useState ([]);

  const Scan = () => {
    fetch('/api/home').then(response => {
      if(response.status === 200){
        return response.json()
      }
    })
    .then(data => setMyjunk(data))
    .then(error => console.log(error))
  }

  const Clear = () => {
    setMyjunk({})
  }

Creating the divs:

{Object.keys(myjunk).map((key) =>{
  return ( 
    <div className='plc-container' key={key} onClick={ReadStuff}>
      <h1>ID:{myjunk[key]['name']}</h1> 
      <h1>IP:{myjunk[key]['IP']}</h1>                      
    </div>
  )

Clicking on the div, just to return a console log returns undefined.

const ReadStuff = () => {
  console.log(this.IP)
}

I eventually want to return the data I have in the 2 h1 tags to a new object (devList) but I can't even get it to console log. Sorry if this is basic but I've been stumped at this for a week. Thanks

I've tried this.IP, myjunk.IP, this,myjunk.IP. myjunk['IP']. Nothing returns. And when I do myjunk.IP I get "cant read from undefined"

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

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

发布评论

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

评论(2

风和你 2025-01-25 04:30:53

做到这一点的一种方法是创建一个单独的组件:

const JunkButton = ({junk}) => (
    <div className='plc-container' key={key} onClick={() => ReadStuff(junk)}>
        <h1>ID:{junk['name']}</h1> 
        <h1>IP:{junk['IP']}</h1>
    </div>
)

现在您的 map()看起来像:

{Object.keys(myjunk).map((key) =>{ <JunkButton junk={ myjunk[key] }/> }

readStuff 变为:

const ReadStuff = (junk) => { console.log(junk) }

请注意,请注意我们如何明确地将事物作为prop或props或功能参数。

One way to do this is to create a separate component:

const JunkButton = ({junk}) => (
    <div className='plc-container' key={key} onClick={() => ReadStuff(junk)}>
        <h1>ID:{junk['name']}</h1> 
        <h1>IP:{junk['IP']}</h1>
    </div>
)

Now your map() looks like:

{Object.keys(myjunk).map((key) =>{ <JunkButton junk={ myjunk[key] }/> }

And ReadStuff becomes:

const ReadStuff = (junk) => { console.log(junk) }

Notice how in React we explicitly pass things around as props or function parameters.

⒈起吃苦の倖褔 2025-01-25 04:30:53

首先,您需要将 myjuck 进行功能,然后像这样安装它:

{Object.keys(myjunk).map((key) =>{
          return (
    // sending myjuck to function whatever that is
            <div className='plc-container' key={key} onClick={() => ReadStuff(myjunk)}>
              <h1>ID:{myjunk[key]['name']}</h1> 
              <h1>IP:{myjunk[key]['IP']}</h1>                      
            </div>
          )

readstuff函数

   const ReadStuff = (myjunk) => { console.log(tmyjunk) }

first you need to pass myjuck to function and then console it like this:

{Object.keys(myjunk).map((key) =>{
          return (
    // sending myjuck to function whatever that is
            <div className='plc-container' key={key} onClick={() => ReadStuff(myjunk)}>
              <h1>ID:{myjunk[key]['name']}</h1> 
              <h1>IP:{myjunk[key]['IP']}</h1>                      
            </div>
          )

ReadStuff function

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