Next.js: render arrow functions breaking in 2.0.0-beta.17

Created on 19 Jan 2017  路  9Comments  路  Source: vercel/next.js

The code below

  • Works in "1.2.3"
  • Breaks in "2.0.0-beta.17"
import React, { Component } from 'react'

export default class App extends Component {
    render = () => <div>Hi</div> //notice the arrow function
}

Errors:

warning.js?1292f09:36 Warning: App(...): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.
warning.js?1292f09:36 Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to compensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server:
 (client) " data-reactid="1"><!-- react-empty: 2 -
 (server) " data-reactid="1"><div data-reactid="2"
bug p0

Most helpful comment

@avanslaars because it's transpiled like the following:

function Component () {
  this.render = function () {}
}

All 9 comments

Something definitely wrong here.
Following code prints it as "Hi"

import React, { Component } from 'react'

export default class App extends Component {
  render() {
    return (<div>Something else</div>)
  }
  render = () => <div>Hi</div> //notice the arrow function
}

@nkzawa this is related to our client side patch-react.js. It works when commented out.
I'll find a fix today.

It also seems like HMR is also not working for class properties. @johnlindquist could you check that with v1.2.3

@arunoda
Changed

import { Component } from 'react'

export default class extends Component {
    state = { message: 'foo' } //notice foo

    render = () => <div>
        {this.state.message}
    </div>
}

To:

import { Component } from 'react'

export default class extends Component {
    state = { message: 'bar' } //notice bar

    render = () => <div>
        {this.state.message}
    </div>
}

And the page updated without refreshing in 1.2.3. So it looks good to me.

@johnlindquist Okay. I'll look into that too.

btw just curious why you'd like to do this since render = () => <div>Hi</div> wouldn't be performant.

@nkzawa curious as to what the specific performance implications are. Would you be able explain or point to a resource so I can understand? Thanks!

@avanslaars because it's transpiled like the following:

function Component () {
  this.render = function () {}
}

@nkzawa Thanks!

Was this page helpful?
0 / 5 - 0 ratings