reactjs - react : validateDOMNesting: #text cannot appear as a child of <tr>

标签 reactjs warnings

你能解释一下为什么 react 显示警告Warning: validateDOMNesting(...): #text cannot appear as a child of <tr>. See Router > RouterContext > CarWashPage > AllCarWashTable > tr > #text. ?我在标签 tr 中没有看到任何文本

呈现表格的代码

export default class AllCarWashTable extends React.Component{

constructor(props) {
    super(props);
    this.generateHeaders = this.generateHeaders.bind(this);
    this.generateRows = this.generateRows.bind(this);
};

static propTypes = {
    cols : React.PropTypes.array.isRequired,
    rows : React.PropTypes.array.isRequired
}

generateHeaders() {
    let cols = this.props.cols;  // [{key, label}]
    return cols.map(function(colData) {
        return <th key={colData.key}> {colData.label} </th>;
    });
}

generateRows() {
    let cols = this.props.cols,  // [{key, label}]
        data = this.props.rows;
    if (this.props.rows.length > 0) {
        return data.map(function(item) {
            var cells = cols.map(function(colData) {
                return <td key={colData.key}> {item[colData.key]} </td>;
            });
            return <tr key={item.id}> {cells} </tr>;
        });
    }
}

render(){
    let headers = this.generateHeaders();
    let rows = this.generateRows();

    return (
         <table className="table table-hove">
             <thead>
                <tr>
                    {headers}
                </tr>
             </thead>
             <tbody>
                    {rows}
             </tbody>

         </table>
    )
}
}

最后,我的表格具有以下结构

enter image description here

问题出在哪里?

最佳答案

当使用逻辑 AND 短路 && 显示/隐藏条件行时也会发生这种情况:

{
  foo && (<tr><td>{foo}</td></tr>)
}

将其更改为三元 a ? b : c 形式,其中 c 为 null 将修复它

{
  foo ? (<tr><td>{foo}</td></tr>) : null
}

关于reactjs - react : validateDOMNesting: #text cannot appear as a child of <tr>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39914455/

相关文章:

Mysql日期警告数据被截断

reactjs - typescript 指向函数

javascript - 如何将事件处理程序传递给 React 中的子组件

javascript - 在输入 onChange 期间 react 改变父状态被卡住

apache-spark - Apache Spark 警告 "Calling spill() on RowBasedKeyValueBatch"的含义

c++ - 在 C++ 中禁用警告

javascript - 我在 Node.js Express 中遇到一些关于路由的问题

javascript - 来自 dangerouslySetInnerHTML 的完全相同的代码不能仅在 Gatsby/ReactJS 页面内工作

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

matlab - 关闭 Octave 中的警告