Skip to content

Instantly share code, notes, and snippets.

@alexkrolick
Last active January 5, 2018 18:22
Show Gist options
  • Save alexkrolick/086accaf29ebd75ac5f1631f31cb9556 to your computer and use it in GitHub Desktop.
Save alexkrolick/086accaf29ebd75ac5f1631f31cb9556 to your computer and use it in GitHub Desktop.
const InjectTimestamp = ({children, ...props}) => {
const time = new Date()
return children(time)
}
InjectTimestamp.propTypes = {
children: PropTypes.func,
}
const Foo = () => {
return (
<div>
<InjectTimestamp>
{ time => <div>time.toLocaleString()</div> }
</InjectTimestamp>
</div>
)
}
const InjectTimestamp = ({render, ...props}) => {
const time = new Date()
return render(time)
}
InjectTimestamp.propTypes = {
render: PropTypes.func,
}
const Foo = () => {
return (
<div>
<InjectTimestamp render={time => (
<div>{ time.toLocaleString() }</div>
)} />
</div>
)
}
@ourmaninamsterdam
Copy link

ourmaninamsterdam commented Jan 5, 2018

https://gist.github.com/alexkrolick/086accaf29ebd75ac5f1631f31cb9556#file-function-as-child-jsx-L14

Noticed a little bug when trying this pattern out - time.toLocaleString() should be wrapped in curly braces, i.e. { time => <div>{time.toLocaleString()}</div> }

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