React: How to Lose the Useless DIV

render() only returns one element. So we get useless DIVs, like:

return (
    <div className="useless">
        <h1>I wanted this</h1>
        <p>I wanted this, too</p>
    </div>
)

But React now has React.Fragment.

import React from 'react'
...
return (
    <React.Fragment>
        <h1>I wanted this</h1>
        <p>I wanted this, too</p>
    </React.Fragment>
)

And when we inspect the result in the browser, we only see the H1 and the P tags.


And there's even a shorter bleeding-edge syntax: <> .... </>

return (
    <>
        <h1>I wanted this</h1>
        <p>I wanted this, too</p>
    </>
)



Comments

Popular posts from this blog

Callback, Promise, Observable, and Doughnuts

Learn grid and grid-template-areas FIRST!!!

The Day TDD Started Working for Me