Skip to content

Instantly share code, notes, and snippets.

@JackHowa
Created May 28, 2018 19:06
Show Gist options
  • Save JackHowa/c6aa2b5708e086cbd81080c9f675cadb to your computer and use it in GitHub Desktop.
Save JackHowa/c6aa2b5708e086cbd81080c9f675cadb to your computer and use it in GitHub Desktop.
react fcc beta code snippets
// React - Bind 'this' to a Class Method
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
visibility: false
};
// change code below this line
this.toggleVisibility = this.toggleVisibility.bind(this)
// change code above this line
}
// change code below this line
toggleVisibility() {
this.setState({
visibility: !(this.state.visibility)
})
}
// change code above this line
render() {
if (this.state.visibility) {
return (
<div>
<button onClick={this.toggleVisibility}>Click Me</button>
<h1>Now you see me!</h1>
</div>
);
} else {
return (
<div>
<button onClick={this.toggleVisibility}>Click Me</button>
</div>
);
}
}
};
@JackHowa
Copy link
Author

https://learn.freecodecamp.org/front-end-libraries/react/render-react-on-the-server-with-rendertostring

  • Can render on the server side
  • Useful perhaps for performance:

Second, this creates a faster initial page load experience because the rendered HTML is smaller than the JavaScript code of the entire app. React will still be able to recognize your app and manage it after the initial load.

  • to reference App, need to use <App />
class App extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return <div/>
  }
};

// change code below this line
ReactDOMServer.renderToString(<App />);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment