我在我的项目中使用 React Material-UI。我在渲染方法中放置了许多组件。然后奇怪的事情发生了。'this.refs.REFNAME' 不能引用某些组件。然后我检查了“this.refs”对象,如下图所示:
如您所见,第一行仅显示了一些可以引用的组件。我不明白为什么。谁能给我解释一下?非常感谢。
更新:感谢詹姆斯,我知道第一行应该是那个时候“this.refs”的状态。但是,为什么有些组件不在 refs 对象中?
这是代码的相关部分:
render(){
return <div>
<Table ref='fromform'>
<TableHeader>
<TableRow>
<TableHeaderColumn tooltip='The ID'>No.</TableHeaderColumn>
<TableHeaderColumn tooltip='The Name'>Name</TableHeaderColumn>
<TableHeaderColumn tooltip='The Status'>Notes</TableHeaderColumn>
</TableRow>
</TableHeader>
<TableBody ref='fromformb'>
{playerList.map(function(player, index){
return <TableRow onTouchTap={this._onShowInfo.bind(this, index)} key={'row' + index}>
<TableRowColumn>{index + 1}</TableRowColumn>
<TableRowColumn>{player.name}</TableRowColumn>
<TableRowColumn>{player.notes}</TableRowColumn>
</TableRow>;
}.bind(this))}
</TableBody>
</Table>
<Dialog
title='Player Info'
ref='playerInfoDialog'>
<form role='form' ref='fromfo'>
<div className='form-group' ref='frowwmformb'>
<TextField type='text' hintText='Player Name' ref='txtName' fullWidth={true} />
<TextField type='text' hintText='Notes' ref='txtNotes' fullWidth={true} />
</div>
</form>
</Dialog>
<MainButtonGroup page='players' />
<Spinner />
</div>;
}
_onShowInfo(index){
var player = playerList[index];
console.log(this.refs);
this.refs.playerInfoDialog.show();
this.refs.txtName.setValue(player.name);
this.refs.txtNotes.setValue(player.notes);
}
}
我想用这段代码做的是在一个带有播放器列表的表中生成行,当我点击一行时,会显示一个包含该行数据的对话框。
但是不能引用引用为“txtName”和“txtNotes”的两个“TextField”(如最后两行,产生错误)。上图由 '_onShowInfo' 方法中的 'console.log' 生成。我添加了一些带有随机名称的 'ref' 只是为了测试。