Skip to content

Instantly share code, notes, and snippets.

@yowchun93
Last active April 5, 2020 05:44
Show Gist options
  • Save yowchun93/8c11c223eb42865707b37ec7578d3a70 to your computer and use it in GitHub Desktop.
Save yowchun93/8c11c223eb42865707b37ec7578d3a70 to your computer and use it in GitHub Desktop.
Render Props
// 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