Javascript 如何获取id的值
我有一个具有Javascript 如何获取id的值,javascript,reactjs,Javascript,Reactjs,我有一个具有id属性的组件,我想获取id的值。这是我的作品 构造函数(道具){ 超级(道具); 此.state={ 数据:[] } 填充(){ for(设i=0;i警报ID ) } } 使用props.id。因为id不是组件tmp本身的属性。它出现在props中,因为id在这里作为prop传递data.push() 试着这样做: constructor(props){ super(props); } getValue(){ alert(this.child_0.props.id
id
属性的组件,我想获取id
的值。这是我的作品
构造函数(道具){
超级(道具);
此.state={
数据:[]
}
填充(){
for(设i=0;i<5;i++){
data.push( )
}
}
getValue(){
设tmp=data[0]
警报(tmp.id)
}
render(){
{
这个。填充
}
返回(
<
按钮onClick={
这个是.getValue
}>警报ID按钮>
)
}
}
使用
props.id
。因为id
不是组件tmp
本身的属性。它出现在props
中,因为id
在这里作为prop
传递data.push()
试着这样做:
constructor(props){
super(props);
}
getValue(){
alert(this.child_0.props.id)
}
render(){
return(
<>
{(new Array[5]).map((item, idx) => <ChildComponent ref={e => this['child_' + idx]} key={idx} id={idx}/>)}
<Button onClick={this.getValue}>alert ID</Button>
</>
)
}
}
构造函数(道具){
超级(道具);
}
getValue(){
警报(this.child\u 0.props.id)
}
render(){
返回(
{(新数组[5]).map((item,idx)=>this['child_uu'+idx]}键={idx}id={idx}/>)}
警报ID
)
}
}
constructor(props){
super(props);
}
getValue(){
alert(this.child_0.props.id)
}
render(){
return(
<>
{(new Array[5]).map((item, idx) => <ChildComponent ref={e => this['child_' + idx]} key={idx} id={idx}/>)}
<Button onClick={this.getValue}>alert ID</Button>
</>
)
}
}