Skip to content

Document that you can't rely on React 16 SSR patching up differences #10591

Closed
@Tarnadas

Description

@Tarnadas

With SSR being loaded on client side, there are various wrong behaviors if the server's HTML differs from the client's HTML.

For a minimal example, I created this repository.
Here is a code snippet:

class AppView extends React.Component {
  render () {
    const isServer = this.props.isServer
    const styles = {
      server: {
        backgroundColor: 'red'
      },
      client: {
        backgroundColor: 'green'
      }
    }
    return (
      <div>
        {
          isServer ?
            <div style={styles.server}>isServer</div> :
            <div style={styles.client}>isClient</div>
        }
      </div>
    )
  }
}

In the example I render a CSS background color of red for the server and green for the client. I force a difference by the server's and client's HTML with the isServer property.

With React 15, everything works as expected: the server renders a red background, but the client corrects it to green. With React 16 however the background stays the same, but the text changes as expected.

There are probably other similar behaviors. For example I found out about this bug, because I was conditionally rendering a complete component like so:

return someCondition && <MyComponent />

It becomes even more weird, because if there is additional JSX after that conditional rendering, it would render that additional JSX as if it is inside that MyComponent

return (
  <div>
    { someCondition && <MyComponent /> }
    <SomeOtherComponent />
  </div>
)

becomes

return (
  <div>
    <MyComponent>
      <SomeOtherComponent />
    </MyComponent>
  </div>
)

if someCondition === true on server side and someCondition === false on client side.

You can see this behavior on my website: http://smmdb.ddns.net/courses
Open Chrome Dev Tools and lower the width until you get the mobile view, then reload the page and see how the list is wrapped inside another Component.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions