Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table rows wrapped in an enclosing tag

Tags:

reactjs

I have a table like this :

return (
            <tr key={key.dataKey}>
                <td>
                    <SuggestBox delay={1000} method={'post'} url={url} onPriceChange={this.handlePrice.bind(this)} index={i}/>
                </td>
                <td>
                    <Quantity refValue={key.quantity} />
                </td>
                <td>
                    <b>{key.price == 0 ? 'Waiting...' : ' $' + key.price.toFixed(2)}</b>
                </td>
                <td>
                    <button className="btn" onClick={this.removeItem.bind(this, i)}> X </button>
                </td>
            </tr>
        )

But When I tried to put another TR in this render function, react gives me wrapping error. But I can't wrap them in a simple div. How to bypass this error ?

What I want is something like that :

return (
            <tr key={key.dataKey}>
                <td>
                    <SuggestBox delay={1000} method={'post'} url={url} onPriceChange={this.handlePrice.bind(this)} index={i}/>
                </td>
                <td>
                    <Quantity refValue={key.quantity} />
                </td>
                <td>
                    <b>{key.price == 0 ? 'Waiting...' : ' $' + key.price.toFixed(2)}</b>
                </td>
                <td>
                    <button className="btn" onClick={this.removeItem.bind(this, i)}> X </button>
                </td>
            </tr>

           //Putting this gives me an error..
            <tr>
              <td colspan="4">Some message</td>
            </tr>
        )
like image 298
T. Cem Yılmaz Avatar asked Nov 27 '25 21:11

T. Cem Yılmaz


1 Answers

Since React 16 you can use Fragments. This example uses the short syntax:

return (
    <>
        <tr key={key.dataKey}>
          <td>
            <SuggestBox delay={1000} method={ 'post'} url={url} onPriceChange={this.handlePrice.bind(this)} index={i}/>
          </td>
          <td>
            <Quantity refValue={key.quantity} />
          </td>
          <td>
            <b>{key.price == 0 ? 'Waiting...' : ' $' + key.price.toFixed(2)}</b>
          </td>
          <td>
            <button className="btn" onClick={this.removeItem.bind(this, i)}>X</button>
          </td>
        </tr>

        //Putting this gives me an error..
        <tr>
          <td colspan="4">Some message</td>
        </tr>
    </>
)

Old Answer:

Wrap them in a <tbody> because you can have multiple tbody tags in a table. According to MDN:

Note that unlike <thead>, <tfoot> and <caption> elements, multiple <tbody> elements are permitted (if consecutive), allowing the data-rows in long tables to be divided into different sections, each separately formatted as needed.

return (
    <tbody>
        <tr key={key.dataKey}>
          <td>
            <SuggestBox delay={1000} method={ 'post'} url={url} onPriceChange={this.handlePrice.bind(this)} index={i}/>
          </td>
          <td>
            <Quantity refValue={key.quantity} />
          </td>
          <td>
            <b>{key.price == 0 ? 'Waiting...' : ' $' + key.price.toFixed(2)}</b>
          </td>
          <td>
            <button className="btn" onClick={this.removeItem.bind(this, i)}>X</button>
          </td>
        </tr>

        //Putting this gives me an error..
        <tr>
          <td colspan="4">Some message</td>
        </tr>
    </tbody>
)
like image 137
Ori Drori Avatar answered Nov 30 '25 11:11

Ori Drori



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!