Created
September 6, 2016 12:48
-
-
Save byrichardpowell/eb9aaef71e38c307ca44bd5afef19cec to your computer and use it in GitHub Desktop.
Show a random element using a React Component (CoffeeScript)
This file contains 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
# 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