Last active
January 5, 2018 18:22
-
-
Save alexkrolick/086accaf29ebd75ac5f1631f31cb9556 to your computer and use it in GitHub Desktop.
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
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> | |
) | |
} |
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
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> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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> }