React-Native - 从子方法调用父方法

时间:2016-12-29 15:01:36

标签: react-native

我有一个Child,Parent Component,我需要从Child Component方法调用Parent Component方法。

CODE

class Social extends Component {
            constructor(props) {
                super(props);

                var tabLabel = this.props.tabLabel;

                this.state = { text: 'Search ' + tabLabel + ' here...' , textInput: ''};
              }
                doneClick() {
                    this.props.showTrans;
                    var request = new XMLHttpRequest();
                    request.onreadystatechange = (e) => {
                      if (request.readyState !== 4) {
                        return;
                      }

                      if (request.status === 200) {
                        alert(request.responseText);
                      } else {
                        console.warn('error');
                      }
                    };

                    request.open('GET', "https://www.googleapis.com/youtube/v3/search?part=snippet&q=tupac&key=AIzaSyDFOz50C_XkuEh2T-2XTN9fqm_5xHYzbg8");
                    request.send();
                }
                      render() {

                        return (
                            <View style={styles.menu} >


                                    <View style={styles.searchbar} >
                                    <Image style={{marginTop: 10 ,marginLeft: 10 , width: 20, height: 20}} source={require('./images/search.png')} />
                                    <TextInput
                                            onSubmitEditing={ this.doneClick }
                                         style={{marginLeft: 5, color: '#757575', width: width - 80, height: 40, fontSize: 11,}}
                                         onChangeText={(textInput) => this.setState({textInput})}
                                         placeholder={this.state.text}
                                          underlineColorAndroid="transparent"
                                         />
                                </View>
                                <View style={styles.sep} >
                                </View>

                            </View>

                        );
                      }
                    }

这是社交子组件

CODE

class ScrollableTabBarView extends Component {

                    constructor(props) {
                        super(props);
                        this.showTrans = this.showTrans.bind(this);
                        this.state = { transWidth: 0,transHeight: 0,tabScroll: null };
                        alert('dddd');

                    }



                    hideTrans() {
                        this.setState({ transHeight: 0, transWidth: 0 });
                    }
                    showTrans() {
                        this.setState({ transHeight: width, transWidth: height });
                    }

                  render() {
                    return (<View style={{ width: width, height: height, position: 'absolute' }}>
                    <ScrollableTabView
                      style={{marginTop: 40, }}
                      initialPage={0}
                      renderTabBar={() => <ScrollableTabBar/ >}
                    >
                      <Text tabLabel='User'>My</Text>
                      <Social  tabLabel='Settings'/>
                      <Social showTrans={ this.showTrans } hideTrans={ this.hideTrans }  tabLabel='Youtube'></Social>
                      <Social tabLabel='Vimeo'>favorite</Social>
                      <Social tabLabel='MySpace'>project</Social>
                    </ScrollableTabView>

                    <View style={{ justifyContent: 'center', alignItems: 'center', top: 0, left: 0,width: this.state.transWidth, height: this.state.transHeight, position: 'absolute', backgroundColor:'rgba(52,52,52,0.5)' }}>
                    <Image style={{width: 30, height: 30}} source={require('./images/loader.gif')}/>

                    </View>

                    </View>)

                  }
                }

父组件。

我如何得到this.props.showTrans来调用

1 个答案:

答案 0 :(得分:3)

你差点叫它。它应该是

doneClick = () => {
  this.props.showTrans();
  ....
}

您可能仍会收到错误,因为将在子组件的范围内调用showTrans。您可能希望将showTrans,hideTrans作为ES2015类属性显示在下面,或者手动绑定它以便&#34;这个&#34;范围是恰当的。

hideTrans = () => {
  this.setState({ transHeight: 0, transWidth: 0 });
}
showTrans = () => {
  this.setState({ transHeight: width, transWidth: height });
}