在我的 react-native 移动应用程序中,我在 row.js 中编写了一个名为 Row 的组件,其中包含一个 TouchableOpacity
与 onClick()
事件处理程序。但是,当单击该组件时,该功能不会运行。
Row 组件显示有关特定电影的一些文本,单击时应运行 handlePress() 函数:
const Row = props => (
<TouchableOpacity onClick={() => props.handlePress(props.imdbID)} style={styles.row}>
<Text>Some text</Text>
</TouchableOpacity>
)
在单独的 app.js 文件中,handlepress 函数已被编写并作为 prop 传递给 Row 组件。 imdbID 变量也从电影对象传递给组件:
handlePress = imdbID => {
// do something with imdbID
}
<Row handlePress={this.handlePress} {...film} />
请有人告诉我我做错了什么以及为什么该功能没有运行。
最佳答案
如果你看看 docs ,它没有 onClick
.
您应该使用 onPress
.
const Row = props => (
// using onPress
<TouchableOpacity onPress={() => props.handlePress(props.imdbID)} style={styles.row}>
<Text>Some text</Text>
</TouchableOpacity>
)
关于javascript - onClick() 不会触发 React-Native App 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59456971/