简体   繁体   English

使用映射函数中的反应挂钩为变量赋值

[英]assign values to variables using react hooks inside map function

If you think this question is stupid, sorry for that and please forgive me because iam Beginner.:)如果您认为这个问题很愚蠢,请原谅我,因为我是初学者。:)

1) I want to declare variables using react hooks i done like this: 1)我想使用我这样做的反应钩子声明变量:

const[adata,setAdata]=useState(   
                    name:"",
                    companyname: ""
                  })

2)I want to assign " name" to the value(name=filteredperson.name,companyname=filteredperson.companyname) that inside map function here, map function code: 2)我想在这里给map函数里面的值(name=filteredperson.name,companyname=filteredperson.companyname)赋值“name”,map函数代码:

    {data.map((filteredPerson, index) => (
<td>{filteredperson.name}</td>
<td>{filteredperson.companyname}</td>

3)Finally,so i assigned my map function values to variables using react hooks, i want to print it on console. 3)最后,所以我使用反应挂钩将我的地图函数值分配给变量,我想在控制台上打印它。

for eg:name:{filteredperson.name} value in console例如:名称:{filteredperson.name} 控制台中的值

Note:filteredperson is a map function.注意:filteredperson 是一个地图函数。 I want to assign map values to variables that are using react hooks.我想将映射值分配给使用反应钩子的变量。

Your data variable is of type {name: string, companyname: string} , so to assign value to this variable, you call the setter function:您的data变量是{name: string, companyname: string}类型,因此要为该变量赋值,请调用 setter 函数:

setData({name: NAME_VALUE, companyname: COMPANYNAME_VALUE})

After you set the value of this object, you can print the values with:设置此对象的值后,您可以使用以下命令打印值:

console.log(data.name) console.log(data.companyname) console.log(data.name) console.log(data.companyname)

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

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