React colspan not working

JavascriptHtmlUser InterfaceDomReactjs

Javascript Problem Overview


Why colspan attribute doesn't have effect in React? I created simple component which renders the following:

<table border="1">
  <tr>
    <th colspan="2">people are...</th>
  </tr>
  <tr>
    <td>monkeys</td>
    <td>donkeys</td>
  </tr>
</table>

and what I get is:

enter image description here

Am I missing something?

Edit: SOLVED

Here is the solution. React expects the attribute name as colSpan, not colspan. Figured this out after wasting ridiculous amount of time to discover this little evil fact.

Javascript Solutions


Solution 1 - Javascript

From React's DOM Differences documentation:

> All DOM properties and attributes (including event handlers) should be camelCased to be consistent with standard JavaScript style.

If you check your browser's console, you'll see that React warns you about this:

<meta charset="UTF-8">
<script src="https://npmcdn.com/[email protected]/dist/react.js"></script>
<script src="https://npmcdn.com/[email protected]/dist/react-dom.js"></script>
<script src="https://npmcdn.com/[email protected]/browser-polyfill.min.js"></script>
<script src="https://npmcdn.com/[email protected]/browser.min.js"></script>
<div id="app"></div>
<script type="text/babel">

var App = React.createClass({
  render() {
    return <table border="1">
      <tbody>
        <tr>
          <th colspan="2">people are...</th>
        </tr>
        <tr>
          <td>monkeys</td>
          <td>donkeys</td>
        </tr>
      </tbody>
    </table>
  }
})

ReactDOM.render(<App who="World"/>, document.querySelector('#app'))

</script>

Warning: Unknown DOM property colspan. Did you mean colSpan?
    in th (created by App)
    in tr (created by App)
    in tbody (created by App)
    in table (created by App)
    in App

Solution 2 - Javascript

In addition to changing the case, I also had to change the value from a string to a number.

Instead of this:

<td colspan='6' />

I had to do this:

<td colSpan={6} />

Solution 3 - Javascript

colspan property is in camelCase like colSpan. So instead of colspan we need to use colSpan.

In React v16.12 you can still supply the value as either int, like so colSpan={4} or string, like so: colSpan="4".

Solution 4 - Javascript

I had a bit of a different case, but the final solution for me was to actually giving the th/td a display: table-cell; property.

Solution 5 - Javascript

I had to put colSpan at the end before closing the opening tag for some reason it wasn't working in the beginning as the first prop.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTuomas ToivonenView Question on Stackoverflow
Solution 1 - JavascriptJonny BuchananView Answer on Stackoverflow
Solution 2 - JavascriptNathan ArthurView Answer on Stackoverflow
Solution 3 - JavascriptPriyanka SharmaView Answer on Stackoverflow
Solution 4 - JavascriptYuval PeretzView Answer on Stackoverflow
Solution 5 - JavascriptRafalfaroView Answer on Stackoverflow