Skip to content

Instantly share code, notes, and snippets.

@DSchau
Created January 4, 2019 02:19
Show Gist options
  • Select an option

  • Save DSchau/b9c72505c383954134a9c673f75eafc5 to your computer and use it in GitHub Desktop.

Select an option

Save DSchau/b9c72505c383954134a9c673f75eafc5 to your computer and use it in GitHub Desktop.
React SSR vs. client-side rendering
import React, { Component } from 'react'
import fetch from 'isomorphic-fetch'
import Layout from '../components/layout'
export default class IndexPage extends Component {
state = {
data: []
}
componentDidMount() {
// this is invoked by the client, e.g. normal React lifecycle method
fetch('/some-data')
.then(res => res.json())
.then(data => {
this.setState({
data: data.items
})
})
}
render() {
return (
<Layout>
<h1>This will be statically rendered</h1>
{this.state.data.length > 0 && (
<React.Fragment>
<h1>This will be rendered on the client</h1>
{
this.state.data.map(item => (
<h2 key={item.id}>{item.text}</h2>
))
}
</React.Fragment>
)}
</Layout>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment