简体   繁体   English

反应本机组件-OnPress函数不起作用

[英]React Native Component - OnPress Function not working

My question is about adding buttons with onPress events in imported components. 我的问题是有关在导入的组件中添加带有onPress事件的按钮。

So I've built an external button component which I'm importing into app.js from a component.js file 因此,我建立了一个外部按钮组件,该component.js将从component.js文件导入到app.js

export default class App extends Component {

  {...}

  render() {
    return (
      <Container style={{ backgroundColor: 'rgb(245, 247, 250)' }}>
        {renderIf(display.beacon, <Beacon />)}
      </Container>
    );
  }
}

This <Beacon /> component displays an image and two buttons imported from another component file: <Beacon />组件显示图像和从另一个组件文件导入的两个按钮:

export class Beacon extends Component {
  render() {
    return (
      <Grid>
        <Row>
        <Image
          source={require("./scenerios/Welcome.png")}
          style={styles.MainContainer}
          />
          </Row>
          <Row style={ {backgroundColor: Blue} } >
          <ButtonWhite onPress={this._startScanning} name={"Start Scanning"} />
          <ButtonWhite onPress={this._stopScanning} name={"Stop Scanning"} />
          </Row>  
        </Grid>
    );
  }
}

When I use these <ButtonWhite /> components in the app render like below they work fine: 当我在应用程序渲染中使用这些<ButtonWhite />组件时,如下所示,它们可以正常工作:

export default class App extends Component {

  {...}

  render() {
    return (
      <Container style={{ backgroundColor: 'rgb(245, 247, 250)' }}>
          <ButtonWhite onPress={this._startScanning} name={"Start Scanning"} />
          <ButtonWhite onPress={this._stopScanning} name={"Stop Scanning"} />
      </Container>
    );
  }
}

The moment I move these to the component, they display on screen but do not call the function. 当我将它们移动到组件时,它们会显示在屏幕上,但不会调用该函数。

avoid to call 避免打电话

 this._stopScanning

once you are moving your component you need to import the callback from your component 移动组件后,您需要从组件中导入回调

import {_stopScanning} from <Component where you have the callbacks>

otherwise you need to refactor the component and just call the function internally instad to receive them as props 否则,您需要重构该组件,并在内部调用instad函数以将其作为道具接收

Solved - Needed to use constructor to reference the function. 已解决-需要使用构造函数来引用该函数。

export class UnknownBeacon extends Component {

  constructor(props) {
    super(props);
    this.state = 
      {
        mainAppRef : props.mainAppRef
      };
  }

  render() {
    return (
      <Grid>
        <Row>
        <Image
          source={require("./scenerios/Welcome.png")}
          style={styles.MainContainer}
         />
         </Row>
         <Row style={ {backgroundColor: tflBlue} } >
         <TfLButtonWhite onPress={this.state.mainAppRef._startScanning} name={"Start Scanning"} />
         <TfLButtonWhite onPress={this.state.mainAppRef._stopScanning} name={"Stop Scanning"} />
         </Row>  
        </Grid>
    );
  }
}

If this is the place where you render the component: 如果这是渲染组件的位置:

{renderIf(display.beacon, <Beacon />)}

Then you want to pass props to the component: 然后,您要将道具传递给组件:

{renderIf(display.beacon, <Beacon
  startScanning={() => this._startScanning()}
  stopScanning ={() => this._stopScanning()}
/>)}

And in Beacon component you want to call the props: Beacon组件中,您要调用道具:

export class Beacon extends Component {
  render() {
    return (
      <Grid>
        <Row>
        <Image
          source={require("./scenerios/Welcome.png")}
          style={styles.MainContainer}
         />
         </Row>
         <Row style={ {backgroundColor: Blue} } >
         <ButtonWhite onPress={this.props.startScanning} name={"Start Scanning"} />
         <ButtonWhite onPress={this.props.stopScanning} name={"Stop Scanning"} />
         </Row>  
        </Grid>
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM