CodeLit.dev
Published in

CodeLit.dev

Understanding Render Props in React

But using an easier example

React Render Props Tutorial
class App extends React.Component {
render() {
return (
<div>
<ul>
<ListItem data={2} />
<ListItem data={7} />
<ListItem data={2} />
<ListItem data={4} />
<ListItem data={9} />
</ul>
</div>
)
}
}
class App extends React.Component {
render() {
return (
<div>
<ul>
<ListItem render={data => <div>{data}</div>} data={2} />
<ListItem render={data => <div><b>{data}</b></div>} data={7} />
<ListItem render={data => <div>{data*3}</div>} data={2} />
<ListItem render={data => <div><i>{data}</i></div>} data={4} />
<ListItem render={data => <div>{`Value: ${data}`}</div>} data={9} />
</ul>
</div>
)
}
}

The render prop is a function which returns a React element.

class ListItem extends React.Component {
render () {
return (
<li>
{this.props.render(this.props.data)}
</li>
)
}
}

Twitter|YouTube|Rayn Studios

--

--

Notes on Software Engineering, DevOps & Cloud

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rajat Saxena

Software Engineer; Builds Internet Based Businesses & Apps; Solopreneur