Created
January 4, 2019 02:19
-
-
Save DSchau/b9c72505c383954134a9c673f75eafc5 to your computer and use it in GitHub Desktop.
React SSR vs. client-side rendering
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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