我正在构建一个React组件,它接受JSON数据源并创建一个可排序的表.
每个动态数据行都有一个分配给它的唯一键,但我仍然收到以下错误:
数组中的每个子节点都应该具有唯一的"键"支柱.
检查TableComponent的render方法.
我的TableComponentrender方法返回:
<table>
<thead key="thead">
<TableHeader columns={columnNames}/>
</thead>
<tbody key="tbody">
{ rows }
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
该TableHeader组件是单行,并且还具有分配给它的唯一键.
每个rowin rows都是使用具有唯一键的组件构建的:
<TableRowItem key={item.id} data={item} columns={columnNames}/>
Run Code Online (Sandbox Code Playgroud)
而TableRowItem看起来像这样:
var TableRowItem = React.createClass({
render: function() {
var td = function() {
return this.props.columns.map(function(c) {
return <td key={this.props.data[c]}>{this.props.data[c]}</td>;
}, this);
}.bind(this);
return (
<tr>{ td(this.props.item) }</tr>
)
}
});
Run Code Online (Sandbox Code Playgroud)
是什么导致了唯一的关键道具错误?
我有一个React组件,我想在单击时切换一个css类.
所以我有这个:
export class myComponent extends React.Component {
constructor() {
super();
this.state = { clicked: false };
this.handleClick = this.handleClick.bind(this);
}
render() {
return (
<div>
<div onClick={this.clicked}><span ref="btn" className="glyphicon"> </span></div>
</div>
);
}
handleClick() {
this.refs.btn.classList.toggle('active');
}
componentDidMount() {
this.refs.btn.addEventListener('click', this.handleClick);
this.setState({
clicked: this.state.clicked = true,
});
}
componentWillUnmount() {
this.refs.btn.removeEventListener('click', this.handleClick);
this.setState({
clicked: this.state.clicked = false,
});
}
}
Run Code Online (Sandbox Code Playgroud)
这个问题是ESLint不断告诉我"this.refs"已经折旧了.
我该怎么做?如何修复它以便不使用折旧代码?
我有一个包含各种输入字段和两个按钮的表单; 一个用于提交,一个用于取消.
<form id="create-course-form">
<input type="text" name="course_Name" ref="fieldName">
<input type="text" name="course_org" ref="fieldOrg">
<input type="text" name="course_Number" ref="fieldNum">
<input type="submit" name="saveCourse" value="Create">
<input type="button" name="cancelCourse" value="cancel" onClick={this.cancelCourse}>
</form>
Run Code Online (Sandbox Code Playgroud)
我想要的是在单击取消按钮时清空所有输入.到目前为止,我已经设法通过使用每个输入的ref prop 来做到这一点.
cancelCourse(){
this.refs.fieldName.value="";
this.refs.fieldorg.value="";
this.refs.fieldNum.value="";
}
Run Code Online (Sandbox Code Playgroud)
但是,我想清空输入字段而不必单独清空每个输入字段.我想要类似的东西(jQuery):$('#create-course-form input[type=text]').val('');