简体   繁体   English

如何访问映射函数中的变量值?

[英]How can I access a value of a variable inside a map function?

In my React app I am populating table rows with data from an array. 在我的React应用程序中,我用来自数组的数据填充表行。

Below, I want to be able to access the value of the notes variable when the year td is clicked 下面,我希望能够在单击年份td时访问notes变量的值

rows.map((row) => {
                        return <tr key={row.id}>
                            <td>{row.start}</td>
                            <td>{row.end}</td>
                            <td  onClick={(row) => console.log(row.year)}>{row.year}</td>
                            <td>{row.notes}</td>
                            <td/></td>
                        </tr>
                    })

When I click this td which has 1999 on screen undefined is output to the console. 当我单击屏幕上具有undefined 1999的td ,将输出到控制台。

Can someone help me understand how I can access this value? 有人可以帮助我了解如何获取此价值吗?

This code fails, why? 此代码失败, 为什么?

rows.map((row) => {
                        return <tr key={row.id}>
                            <td>{row.start}</td>
                            <td>{row.end}</td>
                            <td  onClick={(row) => console.log(row.year)}>{row.year}</td>
                            <td>{row.notes}</td>
                            <td/></td>
                        </tr>
                    })

concretely, onClick={(row) => console.log(row.year)} onClick={(row) => console.log(row.year)}onClick={(row) => console.log(row.year)}

You are assuming that when you do click you get row as a parameter in the onClick callback, what you get there is the event. 您假设当您单击时,在onClick回调中将获得row作为参数,那么所得到的就是事件。 So, your row param is an event which does not have year for that reason you see undefined . 因此,您的row参数是一个没有year的事件,因此您看到undefined

Solution : 解决方案

Just remove row . 只需删除row

onClick={(clickEvent) => console.log(row.year)}

In your arrow function, you have access to the row object. 在箭头功能中,您可以访问row对象。

Create a function like so: 创建一个像这样的函数:

accessNotes(notes) {
console.log(notes)
}

And in your Html do like this: 并在您的HTML中这样做:

rows.map((row) => {
                    return <tr key={row.id}>
                        <td>{row.start}</td>
                        <td>{row.end}</td>
                         // You don't need to pass row
                        <td  onClick={() => {
                                      console.log(row.year);
                                      // here you call the function
                                      this.accessNotes(row.notes)
                                       }}>{row.year}</td>
                        <td>{row.notes}</td>
                        <td/></td>
                    </tr>
                })

You're accessing the year on event object rather than the row. 您正在访问事件对象的年份,而不是行。 The first argument passed to onClick function is the event object which you have also named row ( also known as shadowing ). 传递给onClick函数的第一个参数是事件对象,您也将其命名为row(也称为shadowing)。 Name it something else like event or e or simply omit it if you don't want to access the event. 将其命名为event或e,或者如果您不想访问该事件,则可以将其省略。 Do this. 做这个。

rows.map((row) => {
    return <tr key={row.id}>
        <td>{row.start}</td>
        <td>{row.end}</td>

        <td onClick={(e) => console.log(row.year)}>{row.year}</td>

        <td>{row.notes}</td>
        <td/></td>
    </tr>
})

Hope this helps ! 希望这可以帮助 !

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

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