简体   繁体   English

onClick() 不会触发 React-Native App 中的函数

[英]onClick() doesn't trigger function in React-Native App

In my react-native mobile app I have written a component called Row in row.js that contains a TouchableOpacity with an onClick() event handler.在我反应过来,本地移动应用我写了一个在row.js称为行组件包含TouchableOpacityonClick()事件处理程序。 However when the component is clicked the function doesn't run.但是,当单击该组件时,该功能不会运行。

The Row component displays some text about a particular film and should run the handlePress() function when clicked: Row 组件显示有关特定电影的一些文本,单击时应运行 handlePress() 函数:

const Row = props => (
  <TouchableOpacity onClick={() => props.handlePress(props.imdbID)} style={styles.row}>
    <Text>Some text</Text>
  </TouchableOpacity>
)

In a separate app.js file, the handlepress function has been written and is passed to the Row component as a prop.在单独的 app.js 文件中,handlepress 函数已被编写并作为 prop 传递给 Row 组件。 The imdbID variable is also passed to the component from the film object: imdbID 变量也从电影对象传递给组件:

handlePress = imdbID => {
  // do something with imdbID
}
<Row handlePress={this.handlePress} {...film} />

Please can someone tell me what I am doing wrong and why the function doesn't run.请有人告诉我我做错了什么以及为什么该功能没有运行。

If you take a look at the docs , it doesnt have onClick .如果您查看文档,它没有onClick

You should use onPress .你应该使用onPress

const Row = props => (
  //          using onPress
  <TouchableOpacity onPress={() => props.handlePress(props.imdbID)} style={styles.row}>
    <Text>Some text</Text>
  </TouchableOpacity>
)

React native does not have onClick for TouchableOpacity . React Native 没有用于TouchableOpacity onClick Use onPress instead.请改用onPress

React-Native doesnt provide onClick functionality , it gives onPress instead , so replace onClick with onPress React-Native 不提供onClick功能,而是提供onPress ,因此将 onClick 替换为onPress

const Row = props => (
  <TouchableOpacity onPress={() => props.handlePress(props.imdbID)} style={styles.row}>
    <Text>Some text</Text>
  </TouchableOpacity>
)

hope this helps ,feel free for doubts希望这会有所帮助,不要怀疑

Use onPress() instead of of onClick()使用onPress()而不是onClick()

improved code改进的代码

const Row = props => (
  <TouchableOpacity onPress={()=> props.handlePress(props.imdbID)} style={styles.row}>
    <Text>Some text</Text>
  </TouchableOpacity>
)

使用 onPress 而不是 onClick 并确保您从 react-native 而不是手势处理程序导入可触摸

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

相关问题 react-native中的onClick函数测试按钮 - Test button onClick function in react-native 如果我使用 await,React-Native 应用程序将无法运行 - React-Native app doesn't work if i use await 是否可以通过React-native以编程方式触发WebView的onClick? - Is it possible to programatically trigger an onClick on a WebView with React-native? React-Native自定义组件不会触发onValueChange,onPress或onChange - React-Native Custom Component doesn't trigger onValueChange, onPress, or onChange 在 React 应用程序中单击时,Div onClick function 不起作用 - Div onClick function doesn't work when clicking in React app HTML onclick不会触发功能 - HTML onclick doesn't trigger function React-Native“如果没有测量功能,则无法将没有YogaNode的孩子添加到父母中!” - React-Native 'Cannot add a child that doesn't have a YogaNode to a parent without a measure function!' 为什么 npx react-native init MyApp 不起作用并且不创建 React Native 项目? - Why doesn't npx react-native init MyApp not work and doesn't create a React Native project? 没有函数调用-onclick不起作用 - No function call - onclick doesn't work react 如果从javascript调用,React-native scheduleTimerWithTimeInterval不起作用 - React-native scheduledTimerWithTimeInterval doesn't work if called from javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM