//获取子组件state里内容 父组件 requestUser=()=>{ try{ //this.child 包含子组件方法 与 state里内容 具体可console.log(this.child) var recommendUser=this.child.state.item; console.log(recommendUser) }catch (e) { } } //DOM <MemberLRItem onRef={(ref) => { this.child = ref; }}></MemberLRItem> //子组件 //在此方法里赋值this 即可 componentDidMount() { this.props.onRef(this); }
//子组件调用父方法 和获取父组件 state里内容 //父 //方法 getAddress=(address)=>{ //DOM <SelectAddress setAddress={this.state.censusModel} getAddress={this.getAddress}></SelectAddress> //子组件 //方法 onConfirmClose= key => () => { if(typeof(this.props.getAddress) == 'function'){ this.props.getAddress('子组件的数据传给父'); }else { alert("父组件缺少 getAddress 方法") } } <div onClick={this.onConfirmClose}>调用父方法</div> //即父组件的this.state.censusModel值 {this.props.setAddress}
关注"都市百货" 了解南陵
微信咨询wanglf2r(不拉群 发广告者勿加)
热门评论