react组件如何获取父节点的属性?

1、如下代码:

<FormGroup validationState="success">
    <InputGroup inline>
        <FormControl
            type="text"
            placeholder="请输入"
            value=""
        />
        <TextTip bsStyle="success">我是一行提示信息!</TextTip>
    </InputGroup>
</FormGroup>

2、这里TextTip如何获取FormGroup的属性validationState?(因为我想通过FormGroup的validationState属性来判断TextTip的bsStyle属性)


感谢各位的回答,但是TextTip是在页面上声明的,并不是在FormGroup组件里!!!

阅读 11.7k
3 个回答

FormGroup组件内

constructor(props) {
    super(props)
//...
    this.state = {
        validationState: this.props. validationState
    } 
}


<TextTip bsStyle="success" validationState={this.state.validationState}>我是一行提示信息!</TextTip>

TextTip组件内通过this.props.validationState获得

constructor (props) {
super(props);
this.property2 = this.property2.bind(this);
}

property2(){
return this.props.property2 ;
}

// 子组件需要传值
<TextTip
property1 = { this.props.property1 }
property2 = { this.property2 }
/>

/子组件中接收

const propTypes = {
property1: Proptypes.array.isRequed,
property2: Proptypes.func.isRequed,
}

// 子组件中调用属性方法
<div>
<div onClick={this.props + '加上相应的属性就行' } ></div>
</div>

<FormGroup validationState="success">
    <InputGroup inline { ...this.props }>
        <FormControl
            type="text"
            placeholder="请输入"
            value=""
        />
        <TextTip bsStyle="success" { ...this.props }>我是一行提示信息!</TextTip>
    </InputGroup>
</FormGroup>


// TextTip 中
render () {
    const { validationState } = this.props
    
    return (
        // ...
    )
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题