I'm essentially trying to make tabs in react, but with some issues.
Here's file page.jsx
<RadioGroup>
<Button title="A" />
<Button title="B" />
</RadioGroup>
When you click on button A, the RadioGroup component needs to de-select button B.
"Selected" just means a className from a state or property
Here's 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 radio button that triggers the native DOM onChange
event
The expected flow is:
- Click on Button "A"
- Button "A" triggers onChange, native DOM event, which bubbles up to RadioGroup
- RadioGroup onChange listener is called
- RadioGroup needs to de-select button B. This is my question.
Here's the main problem I'm encountering: I cannot move <Button>
s into RadioGroup
, because the structure of this is such that the children are arbitrary. That is, the markup could be
<RadioGroup>
<Button title="A" />
<Button title="B" />
</RadioGroup>
or
<RadioGroup>
<OtherThing title="A" />
<OtherThing title="B" />
</RadioGroup>
I've tried a few things.
Attempt: In RadioGroup
's onChange handler:
React.Children.forEach( this.props.children, function( child ) {
// Set the selected state of each child to be if the underlying <input>
// value matches the child's value
child.setState({ selected: child.props.value === e.target.value });
});
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
's onChange handler:
React.Children.forEach( this.props.children, function( child ) {
child.props.selected = child.props.value === e.target.value;
});
Problem: Nothing happens, even I give the Button
class a componentWillReceiveProps
method
Attempt: I attempted to pass some specific state of the parent to the children, so I can just update the parent state and have the children respond automatically. In the render 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 react-addons.js cloneWithProps method to clone the children at render time in RadioGroup
to be able to pass them properties
Bad solution #2: Implement an abstraction around HTML / JSX so that I can pass in the properties dynamically (kill me):
<RadioGroup items=[
{ type: Button, title: 'A' },
{ type: Button, title: 'B' }
]; />
And then in RadioGroup
dynamically build these buttons.
This question doesn't help me because I need to render my children without knowing what they are