next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Let there be less divs

Open cmhakkim opened this issue 9 years ago • 8 comments

Now that CSS grid is out and we're finally able to implement sites semantically and lay out stuff nicely without enormous amounts of divs and hacks it would be very nice to see this principle followed in frameworks such as next.js. Unfortunately that's not the case at the moment.

I'm currently following the tutorial and I already have....

<body>
  <div>
    <div id="__next">
      <div data-reactroot="" data-reactid="1" data-react-checksum="315191458">
        <!-- my content full of wrapper divs because of react.js -->
      </div>
      <div id="__next-error"><!-- guess this exists only in dev mode -->
    </div>
  </div>
  <div>
    <script>...</script>
    ...
  </div>
  <script>...</script>
</body>

Expected Behavior

I think only div we should have is the react root div.

Current Behavior

I see a few unnecessary looking divs.

Steps to Reproduce (for bugs)

  1. Follow the first tutorial until it works.

cmhakkim avatar Aug 04 '17 11:08 cmhakkim

Using Bootstrap themes is really difficult because of this. Especially when one needs to use styles like "height:100%" on first child "div" after body.

whogaurav avatar Aug 15 '17 20:08 whogaurav

Added this to normalize:

body>div, #__next, #__next>div, #__next>div>div {
  height: 100%;
}

lancejpollard avatar Oct 15 '17 01:10 lancejpollard

Not sure how CSS Grids is related to this but I'm pretty sure now with React v16 we could get rid of the div with the ID __next and just render an array, what do you think @timneutkens?

sergiodxa avatar Oct 15 '17 02:10 sergiodxa

@sergiodxa You're right 👍

timneutkens avatar Oct 15 '17 10:10 timneutkens

would recommend a small edit to the workaround by @lancejpollard:

/* normalize divs surrounding the content*/
      body > div:first-child,
      #__next,
      #__next > div,
      #__next > div > div {
        height: 100%;
      }

This is to avoid the #__next-error div blowing up to 100% as well.

This is pretty darn hacky though. :( I would rlly appreciate it if the div inside of body could at least have a className, so I don't have to try to target it using body > div:first-child which feels bad to do

jul-sh avatar Nov 11 '17 18:11 jul-sh

@laurajuliette Thank you for a quick hack, it would indeed be nice if the divs had classes so you could make them 100% height easier

kevinsimper avatar Dec 14 '17 13:12 kevinsimper

Why can't this be adjusted? I don't see the point for generating the divs. The new React fragment syntax should also be possible to use:

class X extends React.Component {
  render() {
    return (
      <>
        <td>Hello</td>
        <td>World</td>
      </>
    );
  }
}

https://reactjs.org/docs/fragments.html

julkue avatar Feb 01 '18 09:02 julkue

Fixed in canary. <> can't be used yet because it requires the beta version of babel. Which we'll integrate in next.js v6.

timneutkens avatar Feb 01 '18 09:02 timneutkens