简体   繁体   English

具有forEach的多个按钮,同时还具有onClick句柄ReactJS

[英]Multiple Button with forEach while also accesing onClick handle ReactJS

I have a button which has the properties of label and onClick . 我有一个具有labelonClick属性的按钮。 I also have an array with the value I need to pass to the label properties. 我还有一个数组,该数组的值需要传递给标签属性。 Here's the code : 这是代码:

render(){
{tabel_soal.forEach(function (item, index) {
                <RaisedButton
                    label={item}
                    onClick={this.handleTabelSoalClick}
                    primary={true}
                />
            })}
}

With the array in following structure : 具有以下结构的数组:

tabel_soal ={"buttonName1","buttonName2","buttonName3"}

But I also need to access the onClick properties to run following function : 但是我还需要访问onClick属性以运行以下功能:

handleTabelSoalClick = () =>{
    const {push} = this.props
    push(`/tabelsampel/getDetailTabel/`+this.state.tabel_soal)
}

But currently it showed error that this is not defined. 但是目前,它显示了未定义的错误。

Is there somewhere I did wrong? 我做错了什么地方吗? Any help will be appreciated 任何帮助将不胜感激

To get a button do the following and its onclick or the label you can pass the array 要获取按钮,请执行以下操作及其onclick或标签,您可以传递数组

<RaisedButton
label={tabel_soal}
onClick={()=>this.handleTabelSoalClick(tabel_soal)}
primary={true}
 />

and the function will receive the array in param, 该函数将在param中接收数组,

handleTabelSoalClick = (tabel_soal) =>{
    ...//what ever you need to do with the array.
}

If you wanna get multiple buttons instead, you can use the following code, 如果您想获得多个按钮,则可以使用以下代码,

render(){
{tabel_soal.map((item, index) =><RaisedButton
                    label={item}
                    onClick={this.handleTabelSoalClick}
                    primary={true}
                />
       )}
}

First of all, you declared the wrong object. 首先,您声明了错误的对象。

tabel_soal ={"buttonName1","buttonName2","buttonName3"} tabel_soal = {“ buttonName1”,“ buttonName2”,“ buttonName3”}

It should be like this 应该是这样

 tabel_soal = {
     "buttonName1": { name: "buttonName1", id: 1 },
     "buttonName2": { name: "buttonName2", id: 2 },
     "buttonName3": { name: "buttonName3", id: 3 }
 }

or 要么

 tabel_soal = {
     "buttonName1": "buttonName1",
     "buttonName2": "buttonName2",
     "buttonName3": "buttonName3"
 }

or 要么

declared as an array if you want the only key. 如果需要唯一键,则声明为数组。

tabel_soal = ["buttonName1","buttonName2","buttonName3"]

If you use the first object type then you can do something like this. 如果使用第一种对象类型,则可以执行以下操作。

render(){
{tabel_soal.forEach(function (item, index) {
                <RaisedButton
                    label={item}
                    onClick={(e)=>this.handleTabelSoalClick(item)}
                    primary={true}
                />
            })}
}

And in click event handler you can use the value like this. 在点击事件处理程序中,您可以使用类似这样的值。

handleTabelSoalClick = (item) =>{
   .....(`/tabelsampel/getDetailTabel/`+ item.name)
   or
   .....(`/tabelsampel/getDetailTabel/`+ item.id)

}

If you use the second object type then you can do something like this. 如果使用第二种对象类型,则可以执行以下操作。

render(){
{tabel_soal.forEach(function (name, index) {
                <RaisedButton
                    label={name}
                    onClick={()=>this.handleTabelSoalClick(name)}
                    primary={true}
                />
            })}
}

And in click event handler you can use the value like this. 在点击事件处理程序中,您可以使用类似这样的值。

handleTabelSoalClick = (name) =>{
   .....(`/tabelsampel/getDetailTabel/`+ name)    
}

If you use the third array type then you can do something like this. 如果使用第三种数组类型,则可以执行以下操作。

render(){
{tabel_soal.forEach(function (name, index) {
                <RaisedButton
                    label={name}
                    onClick={()=>this.handleTabelSoalClick(name,index)}
                    primary={true}
                />
            })}
}

And in click event handler you can use the value like this. 在点击事件处理程序中,您可以使用类似这样的值。

handleTabelSoalClick = (name,index) =>{
   .....(`/tabelsampel/getDetailTabel/`+ name)   
  or 
   .....(`/tabelsampel/getDetailTabel/`+ tabel_soal[index]) 

}

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

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