"Действительный элемент React (или null) должен быть возвращен" с одним из компонентов в ReactJS
Мой код похож на этот
var data = [
{id: 1, taskName: "Pete Hunt", standarDescription: "This is one comment", emplComment: "meaaow I am meeawo", empRating : "1"},
{id: 2, taskName: "Pete Hunt", standarDescription: "This is one comment", emplComment: "meaaow I am meeawo", empRating : "1"},
{id: 3, taskName: "Pete Hunt", standarDescription: "This is one comment", emplComment: "meaaow I am meeawo", empRating : "1"},
{id: 4, taskName: "Pete Hunt", standarDescription: "This is one comment", emplComment: "meaaow I am meeawo", empRating : "1"},
{id: 5, taskName: "Pete Hunt", standarDescription: "This is one comment", emplComment: "meaaow I am meeawo", empRating : "1"}
];
var Tableforbasictask = React.createClass({
render: function() {
return (
<div className="downloadlinks">
<table className="table table-bordered table-striped-col nomargin" id="table-data">
<tbody>
<tr align="center">
<td>Task Name</td>
<td>Standard Discription of Task</td>
<td>Employee Comment</td>
<td>Employee rating</td>
</tr>
<TableforbasictaskList data={this.props.data} />
<TableforbasictaskForm />
</tbody>
</table>
</div>
);
}
});
var TableforbasictaskForm = React.createClass({
render: function() {
return (
<div className="commentForm">
Hello, world! I am a CommentForm.
</div>
);
}
});
var Addcontenttotable = React.createClass({
render: function() {
return (
<tr><td>{this.props.taskName}</td>
<td>{this.props.standarDescription}</td>
<td>{this.props.emplComment}</td>
<td>{this.props.empRating}</td>
</tr>
);
}
});
var TableforbasictaskList = React.createClass({
render: function() {
var commentNodes = this.props.data.map(function(comment) {
return (
<Addcontenttotable taskName={comment.taskName} standarDescription={comment.standarDescription} emplComment={comment.emplComment} empRating={comment.empRating} key={comment.id}>
</Addcontenttotable>
);
});
return (
{commentNodes}
);
}
});
ReactDOM.render(<div><Tableforbasictask data={data} /></div>, document.getElementById('content'));
Все, что я пытаюсь сделать, - это перечислить данные из данных Json
в таблицу. Я буду добавлять API для получения этого JSON в будущем
но я получаю следующую ошибку
Ошибка: TableforbasictaskList.render(): должен быть возвращен действительный элемент React (или null). Возможно, вы вернули undefined, массив или какой-либо другой недопустимый объект.
Вот JSFIDDLE
Любая помощь приветствуется
Ответы
Ответ 1
У компонента React должен быть только один корневой узел. Так как вы используете TableforbasictaskList
внутри table
вам нужно обернуть commentNodes
в <tbody>
. Также внутри Tableforbasictask
переместите TableforbasictaskForm
из table
var TableforbasictaskList = React.createClass({
render: function() {
// .....
return (<tbody>{commentNodes}</tbody>);
}
});
var Tableforbasictask = React.createClass({
render: function() {
return <div className="downloadlinks">
<table
className="table table-bordered table-striped-col nomargin"
id="table-data"
>
<thead>
<tr align="center">
<td>Task Name</td>
<td>Standard Discription of Task</td>
<td>Employee Comment</td>
<td>Employee rating</td>
</tr>
</thead>
<TableforbasictaskList data={this.props.data} />
</table>
<TableforbasictaskForm />
</div>
}
});
Example
Ответ 2
render
должен возвращать один корневой элемент https://jsfiddle.net/69z2wepo/41120/
return (<div>
{commentNodes}
</div>);
Update. Более допустимый вариант с использованием tbody в качестве обертки
return (<tbody>
{commentNodes}
</tbody>);
https://jsfiddle.net/69z2wepo/41125/