[英]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称为行组件包含TouchableOpacity
用onClick()
事件处理程序。 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.请有人告诉我我做错了什么以及为什么该功能没有运行。
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.