Last active
April 5, 2020 05:44
-
-
Save yowchun93/8c11c223eb42865707b37ec7578d3a70 to your computer and use it in GitHub Desktop.
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
// A component with render prop takes a function that returns React element and calls it instead of | |
// implementing its own render | |
// render, props is a function | |
// render prop is a funtion that takes in data, and return JSX element? | |
<DataProvider render={data => ( | |
<h1>Hello {data.target} </h1> | |
)}/> | |
// instead rendering from SharedComponent, you render using props. | |
// Reusable component i guess | |
const SharedComponent extends React.Component { | |
render() { | |
return( | |
<div> | |
{this.props.render()} | |
</div> | |
) | |
} | |
} | |
// functional component | |
const SharedComponent = ({ render }) => { | |
return( | |
<div> | |
{this.props.render()} | |
</div> | |
) | |
} | |
const SayHello = () => ( | |
<SharedComponent render={() => ( | |
<span>hello!</span> | |
)} /> | |
) | |
// render Props in TS | |
interface FormProps<T> { | |
values: T; | |
children: (values: T) => JSX.Element; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment