admin管理员组文章数量:1431298
In my react-native mobile app I have written a ponent called Row in row.js that contains a TouchableOpacity
with an onClick()
event handler. However when the ponent is clicked the function doesn't run.
The Row ponent displays some text about a particular film and should run the handlePress() function when clicked:
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 ponent as a prop. The imdbID variable is also passed to the ponent from the film object:
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.
In my react-native mobile app I have written a ponent called Row in row.js that contains a TouchableOpacity
with an onClick()
event handler. However when the ponent is clicked the function doesn't run.
The Row ponent displays some text about a particular film and should run the handlePress() function when clicked:
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 ponent as a prop. The imdbID variable is also passed to the ponent from the film object:
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.
Share Improve this question asked Dec 23, 2019 at 14:28 M. AlexM. Alex 7331 gold badge9 silver badges33 bronze badges 05 Answers
Reset to default 4If you take a look at the docs, it doesnt have onClick
.
You should use onPress
.
const Row = props => (
// using onPress
<TouchableOpacity onPress={() => props.handlePress(props.imdbID)} style={styles.row}>
<Text>Some text</Text>
</TouchableOpacity>
)
React-Native doesnt provide onClick
functionality , it gives onPress
instead , so replace onClick with 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()
improved code
const Row = props => (
<TouchableOpacity onPress={()=> props.handlePress(props.imdbID)} style={styles.row}>
<Text>Some text</Text>
</TouchableOpacity>
)
Use onPress instead of onClick and be sure you are importing touchable from react-native not gesture-handler
React native does not have onClick
for TouchableOpacity
.
Use onPress
instead.
本文标签: javascriptonClick() doesn39t trigger function in ReactNative AppStack Overflow
版权声明:本文标题:javascript - onClick() doesn't trigger function in React-Native App - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745572103a2664107.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论