好比antd里的RadioGroup和Radio<RadioGroup onChange={this.onChange} value={this.state.value}>
<Radio value={1}>A</Radio>
<Radio value={2}>B</Radio>
<Radio value={3}>C</Radio>
<Radio value={4}>D</Radio></RadioGroup>是如何做到点击Radio的时候触发了onChange呢?
1 回答

哈士奇WWW
TA贡献1799条经验 获得超6个赞
这个东西看下源码就知道了 通过Context
传递
Group:
static childContextTypes = { radioGroup: PropTypes.any, };getChildContext() { return { radioGroup: { onChange: this.onRadioChange,//内部调用了this.props.onChange }, }; }
Radio:
static contextTypes = { radioGroup: PropTypes.any, }; render(){ //这里就获取到onChange事件了 radioProps.onChange = this.context.radioGroup.onChange; }
添加回答
举报
0/150
提交
取消