Created
November 8, 2016 07:27
-
-
Save quangnd/a8305c528882efb4d231107b01c1ddc5 to your computer and use it in GitHub Desktop.
Send Props to Children in React
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
//Original link: http://jaketrent.com/post/send-props-to-children-react/ | |
function RadioOption(props) { | |
return ( | |
<label> | |
<input type="radio" value={props.value} name={props.name} /> | |
{props.label} | |
</label> | |
) | |
} | |
function renderChildren(props) { | |
return React.Children.map(props.children, child => { | |
if (child.type === RadioOption) | |
return React.cloneElement(child, { | |
name: props.name | |
}) | |
else | |
return child | |
}) | |
} | |
function RadioGroup(props) { | |
return ( | |
<div class="radio-group"> | |
{renderChildren(props)} | |
</div> | |
) | |
} | |
function WhereImUsingRadioGroups() { | |
return ( | |
<RadioGroup name="blizzard-games"> | |
<RadioOption label="Warcraft 2" value="wc2" /> | |
<RadioOption label="Warcraft 3" value="wc3" /> | |
<RadioOption label="Starcraft 1" value="sc1" /> | |
<RadioOption label="Starcraft 2" value="sc2" /> | |
</RadioGroup> | |
) | |
} | |
ReactDOM.render( | |
<WhereImUsingRadioGroups />, | |
document.getElementById('container') | |
); | |
//Link demo: http://react.jsbin.com/zewefelige/1/edit?html,css,js,output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment