I am essentially trying to make tabs in the reaction, but with some problems.
Here is the page.jsx file
<RadioGroup> <Button title="A" /> <Button title="B" /> </RadioGroup>
When you press the A button, the RadioGroup component needs to deselect the B button .
"Selected" means only className from state or property
Here is RadioGroup.jsx :
module.exports = React.createClass({ onChange: function( e ) { // How to modify children properties here??? }, render: function() { return (<div onChange={this.onChange}> {this.props.children} </div>); } });
The source of Button.jsx doesn't really matter; it has a regular HTML button that fires its own DOM onChange event
Expected Stream:
- Press the button "A"
- Button "A" fires onChange, a native DOM event that bubbles up to RadioGroup
- RadioGroup onChange listener called
- The radio group needs to deselect button B. This is my question.
Here is the main problem I am facing: I cannot move the <Button> to the RadioGroup , because the structure of this is such that the children are arbitrary . That is, the markup may be
<RadioGroup> <Button title="A" /> <Button title="B" /> </RadioGroup>
or
<RadioGroup> <OtherThing title="A" /> <OtherThing title="B" /> </RadioGroup>
I have tried several things.
Attempt: In RadioGroup onChange handler:
React.Children.forEach( this.props.children, function( child ) {
Problem:
Invalid access to component property "setState" on exports at the top level. See react-warning-descriptors . Use a static method instead: <exports />.type.setState(...)
Attempt: In RadioGroup onChange handler:
React.Children.forEach( this.props.children, function( child ) { child.props.selected = child.props.value === e.target.value; });
Problem: Nothing happens, even I suggest Button a componentWillReceiveProps method
Attempt: I tried to pass on a specific state of the parent element to the children, so I can just update the parent state and give the children an answer automatically. In the rendering function of RadioGroup:
React.Children.forEach( this.props.children, function( item ) { this.transferPropsTo( item ); }, this);
Problem:
Failed to make request: Error: Invariant Violation: exports: You can't call transferPropsTo() on a component that you don't own, exports. This usually means you are calling transferPropsTo() on a component passed in as props or children.
Bad solution # 1 : use the response-addons.js cloneWithProps method to clone children during rendering in RadioGroup to be able to pass properties to them
Bad solution # 2 : implement an abstraction around HTML / JSX so that I can dynamically pass properties (kill me):
<RadioGroup items=[ { type: Button, title: 'A' }, { type: Button, title: 'B' } ]; />
And then in RadioGroup dynamically create these buttons.
This question does not help me, because I need to display my children without knowing what they are.