Created
February 27, 2019 03:46
-
-
Save YannickLeRoux/f59361a12dae4fe24191f772785c926d to your computer and use it in GitHub Desktop.
Understand React Render Props
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 from "react"; | |
import ReactDOM from "react-dom"; | |
import "./styles.css"; | |
class Toggle extends React.Component { | |
state = { on: false }; | |
toggle = () => this.setState({ on: !this.state.on }); | |
render() { | |
return this.props.children({ on: this.state.on, toggle: this.toggle }); | |
} | |
} | |
// React.createElement(component, props, ...children) | |
class App extends React.Component { | |
render() { | |
return React.createElement(Toggle, null, ({ on, toggle }) => | |
React.createElement("button", { onClick: toggle }, on ? "Hello" : "Bye") | |
); | |
} | |
} | |
const rootElement = document.getElementById("root"); | |
ReactDOM.render(<App />, rootElement); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment