最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React <div> cannot appear as a child of <tr> Warning - Stack Overflow

programmeradmin3浏览0评论

I am getting the following warnings in a React component:

The related code is the following:

import React, { PropTypes } from 'react';
import { Checkbox } from 'react-bootstrap'; 

const MyComponent = (params) => {

function onSelect(event) {
    params.onSelect(event, params.data.id);
}

return (
    <tr key={params.data.id}>
        {params.isSelectable ? (
            <Checkbox onChange={onSelect}>
                <td>{params.data.firstName}&nbsp;</td>
                <td>{params.data.lastName}&nbsp;</td>
            </Checkbox>
        ) : (
            <div>
                <td>{params.data.firstName}&nbsp;</td>
                <td>{params.data.lastName}&nbsp;</td>
            </div>
        )}
    </tr>
);

};

If I remove the div tags, I get the following error:

Adjacent JSX elements must be wrapped in an enclosing tag

I am new to React, so i am not quite clear on what is going on here. What's the problem and how can I fix it?

Update: my React version is 15.3.2.

I am getting the following warnings in a React component:

The related code is the following:

import React, { PropTypes } from 'react';
import { Checkbox } from 'react-bootstrap'; 

const MyComponent = (params) => {

function onSelect(event) {
    params.onSelect(event, params.data.id);
}

return (
    <tr key={params.data.id}>
        {params.isSelectable ? (
            <Checkbox onChange={onSelect}>
                <td>{params.data.firstName}&nbsp;</td>
                <td>{params.data.lastName}&nbsp;</td>
            </Checkbox>
        ) : (
            <div>
                <td>{params.data.firstName}&nbsp;</td>
                <td>{params.data.lastName}&nbsp;</td>
            </div>
        )}
    </tr>
);

};

If I remove the div tags, I get the following error:

Adjacent JSX elements must be wrapped in an enclosing tag

I am new to React, so i am not quite clear on what is going on here. What's the problem and how can I fix it?

Update: my React version is 15.3.2.

Share Improve this question edited Dec 20, 2017 at 19:54 lukegf asked Dec 20, 2017 at 17:15 lukegflukegf 2,2373 gold badges29 silver badges40 bronze badges 6
  • 3 Divs as children of table rows is invalid HTML. Only table headers or table cells can be children of rows. – j08691 Commented Dec 20, 2017 at 17:18
  • <td> belong inside <tr> only. The clue is in the question. Also <div> should be within a <td> or <th> if required to be within table contents – pokeybit Commented Dec 20, 2017 at 17:19
  • Show your Checkbox component...Try to replicate your problem here – Dhaval Jardosh Commented Dec 20, 2017 at 17:30
  • @DhavalJardosh my Checkbox component is from the 'react-bootstrap' library. I didn't create it. – lukegf Commented Dec 20, 2017 at 19:30
  • This is what you are talking about, right? – Dhaval Jardosh Commented Dec 20, 2017 at 19:38
 |  Show 1 more comment

2 Answers 2

Reset to default 6

There are two problems with your code

  1. Only td and th are allowed inside tr
  2. In React version < 15, you have to wrap tags in one element, when you try to render them. In React 16, you can now do the following :

    [
      <td key={1}>{params.data.firstName}&nbsp;</td>,
      <td key={2}>{params.data.lastName}&nbsp;</td>
    ]
    

    instead of wrapping the tds inside a div

I also suggest you extract your logic outside of the tr

If you need to return several elements and can't have a wrapper (such as in this case), you have a new option as of React 16.2. Fragments:

<React.Fragment>
  <td>{params.data.firstName}&nbsp;</td>
  <td>{params.data.lastName}&nbsp;</td>
</React.Fragment>

Or, you might be able to simplify it as:

<>
  <td>{params.data.firstName}&nbsp;</td>
  <td>{params.data.lastName}&nbsp;</td>
</>

The fragments won't resolve to any HTML element, so the <td>s will be direct children of your <tr>.

发布评论

评论列表(0)

  1. 暂无评论