Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React colspan not working

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.

like image 560
Tuomas Toivonen Avatar asked Jul 11 '16 08:07

Tuomas Toivonen


People also ask

How does colSpan work in HTML?

The colspan attribute in HTML specifies the number of columns a cell should span. It allows the single table cell to span the width of more than one cell or column. It provides the same functionality as “merge cell” in a spreadsheet program like Excel.


1 Answers

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 
like image 141
Jonny Buchanan Avatar answered Sep 23 '22 09:09

Jonny Buchanan