【问题标题】:How to call a parent function within child in Native React如何在 Native React 中调用子函数中的父函数
【发布时间】:2018-03-28 10:06:05
【问题描述】:

花了几个小时在谷歌上搜索却没有找到答案......我正在寻求你的帮助。

所以我想做的是:在子级中调用名为 _toggleSearchBar() 的函数(在父级中),这样当 onPress 事件(在子级中)触发时,它会更改父级中的值 'isVisible' .

家长

class HomeScreen extends React.Component {

  constructor(props) {
    super(props);

    this.state = {isVisible: false};
  }

  static navigationOptions = {
    title: 'P O S T E R',
    headerStyle: { backgroundColor: '#CECECE' },
    headerTitleStyle: { color: 'black', fontSize: 30, fontFamily: 'HelveticaNeue-CondensedBlack'},
    headerRight: <DisplayIcon src={require('./ressources/icon_search.png')} myMethod={'HERE'}/>,
    headerLeft: <DisplayIcon src={require('./ressources/icon_aroundMe.png')}/>,
  };
  render() {
    const { navigate } = this.props.navigation;
    return (
      <View>
          <View style={styles.bck}>
          <ScrollView>
            <DisplayImage src={require('./ressources/logo.jpg')} />
            <DisplayImage src={require('./ressources/logo1.jpeg')} />
            <DisplayImage src={require('./ressources/logo2.jpg')} />
            <DisplayImage src={require('./ressources/logo3.jpeg')} />
            <DisplayImage src={require('./ressources/logo4.jpg')} />
            <DisplayImage src={require('./ressources/logo5.jpeg')} />
            <DisplayImage src={require('./ressources/bde.jpeg')} />
          </ScrollView>
        </View>
        <Display enable={this.state.isVisible} style={styles.ViewIn}>
          <View>
            <TextInput style={styles.textIn}></TextInput>
          </View>
        </Display>
      </View>
    )
  }
  _toggleSearchBar() {
    this.setState(previousState => {
      return { isVisible: !this.state.isVisible };
    });
  }
}

儿童

class DisplayIcon extends React.Component {

  constructor(props) {
    super(props);
  }

  render() {
    return (
      <TouchableHighlight onPress={this.myMethod} activeOpacity= {0.4} underlayColor={ 'rgb(206, 206, 206)' }>
        <Image style={styles.Picture} source={this.props.src}/>
      </TouchableHighlight>
    );
  }
}

const styles = StyleSheet.create({
  Picture: {
    marginLeft: 10,
    marginRight: 10,
    height: 30,
    width: 30,
  }
});

绑定不起作用。也不通过 props 传递函数...

感谢您的帮助和时间!

【问题讨论】:

  • 请不要发布您的代码图片。将您的代码直接添加到问题中,以帮助人们帮助您。
  • 好吧,我的错
  • DisplayIcon 组件位于层次结构中的什么位置?因为我在您的 HomeScreen 中看到的只是 DisplayImage。 DisplayImage 和 DisplayIcon 是同一个组件吗?

标签: function reactjs react-native parent-child native


【解决方案1】:

帮助理解

在孩子的内心。

这不起作用(通过父函数)

<TouchableHighlight onPress={this.props.myMethod} activeOpacity= {0.4} underlayColor={ 'rgb(206, 206, 206)' } style={styles.lol}>
        <Image style={styles.Picture} source={this.props.src}/>
</TouchableHighlight>

这是有效的(通过子函数)

  lol() {
    alert('lol');
  }

  render() {
    return (
      <TouchableHighlight onPress={this.lol} activeOpacity= {0.4} underlayColor={ 'rgb(206, 206, 206)' } style={styles.lol}>
        <Image style={styles.Picture} source={this.props.src}/>
      </TouchableHighlight>

【讨论】:

    【解决方案2】:

    Child 组件中,您应该调用 this.props.myMethod 而不是 this.myMethod
    示例:

    <TouchableHighlight onPress={this.props.myMethod} activeOpacity= {0.4} underlayColor={ 'rgb(206, 206, 206)' }>
    

    在您的父母中,您应该将道具传递给孩子 - myMethod={this._toggleSearchBar}
    示例:

    <DisplayIcon src={require('./ressources/icon_search.png')} myMethod={this._toggleSearchBar}/>
    

    请注意,您应该将_toggleSearchBar 绑定到class
    constructor做吧:

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

    【讨论】:

    • 没有错误,但没有出现预期的错误。我猜我的代码是错误的,所以我将函数更改为 alert('azert'); ...仍然没有错误,但是当我单击时我没有传递函数,因为警报没有显示
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 2020-02-27
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    相关资源
    最近更新 更多