【发布时间】:2018-02-04 14:07:44
【问题描述】:
我有一个状态变化的组件,我想从父组件中获取“性别”的值。我该怎么做?
class CardSegment extends Component {
constructor(props) {
super(props);
this.state = { gender: 'male', val: true };
}
render() {
return (
<StyleProvider style={getTheme(commonColor)}>
<Segment style={styles.SegmentStyle}>
<Button
active={this.state.val}
onPress={() => this.setState({ gender: 'male', val: true })}
style={styles.SegmentBtnStyle}
>
<Text style={styles.textStyle}>Male</Text>
</Button>
<Button
active={!this.state.val}
onPress={() => this.setState({ gender: 'female', val: false })}
style={styles.SegmentBtnStyle}
>
<Text style={styles.textStyle}>Female</Text>
</Button>
</Segment>
</StyleProvider>
);
}
}
【问题讨论】:
-
把它作为道具传给孩子?
-
您的子组件在哪里?您始终可以将其作为道具传递,如下所述:reactjs.org/docs/components-and-props.html
-
CardSegment 是子组件,我想从父组件中获取性别的值。
标签: reactjs react-native jsx