Skip to content

Instantly share code, notes, and snippets.

@byrichardpowell
Created September 6, 2016 12:48
Show Gist options
  • Save byrichardpowell/eb9aaef71e38c307ca44bd5afef19cec to your computer and use it in GitHub Desktop.
Save byrichardpowell/eb9aaef71e38c307ca44bd5afef19cec to your computer and use it in GitHub Desktop.
Show a random element using a React Component (CoffeeScript)
# COMPONENT
RandomChild = React.createClass
displayName: "RandomChild"
getInitialState: -> {
index: Math.ceil(Math.random() * @props.children.length-1)
}
render: ->
return (
<div className="show-children-in-sequence-base">
{@props.children[@state.index]}
</div>
)
return RandomChild
# USAGE
<ShowRandomChild>
<div className="some class">Message 1 content</div>
<div className="some class">Message 2 content</div>
<div className="some class">Message 3 content</div>
<div className="some class">Message 4 content</div>
</ShowRandomChild>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment