admin管理员组文章数量:1420095
So I am using react-native-router-flux
for my navigation in my ReactJS app but I am having difficulties using it with ponents.
For example the following code snippet works perfectly by pushing the next page when clicking the links.
export default class MainMenuPage extends Component {
render() {
return (
<View>
<Text>Main Menu</Text>
<TouchableOpacity onPress={Actions.bookmarksPage}>
<Text>Bookmarks</Text>
</TouchableOpacity>
<TouchableOpacity onPress={Actions.settingsPage}>
<Text>Settings</Text>
</TouchableOpacity>
</View>
);
}
}
While the following code snippet nothing happens when you press the links.
export default class MainMenuPage extends Component {
render() {
return (
<View>
<Text>Main Menu</Text>
<MenuButton name="Bookmarks" onPress={Actions.bookmarksPage} />
<MenuButton name="Settings" onPress={Actions.settingsPage} />
</View>
);
}
}
class MenuButton extends Component {
render() {
return(
<TouchableOpacity>
<Text>{this.props.name}</Text>
</TouchableOpacity>
);
}
}
Obviously the second code snippet is much more cleaner.
So I am using react-native-router-flux
for my navigation in my ReactJS app but I am having difficulties using it with ponents.
For example the following code snippet works perfectly by pushing the next page when clicking the links.
export default class MainMenuPage extends Component {
render() {
return (
<View>
<Text>Main Menu</Text>
<TouchableOpacity onPress={Actions.bookmarksPage}>
<Text>Bookmarks</Text>
</TouchableOpacity>
<TouchableOpacity onPress={Actions.settingsPage}>
<Text>Settings</Text>
</TouchableOpacity>
</View>
);
}
}
While the following code snippet nothing happens when you press the links.
export default class MainMenuPage extends Component {
render() {
return (
<View>
<Text>Main Menu</Text>
<MenuButton name="Bookmarks" onPress={Actions.bookmarksPage} />
<MenuButton name="Settings" onPress={Actions.settingsPage} />
</View>
);
}
}
class MenuButton extends Component {
render() {
return(
<TouchableOpacity>
<Text>{this.props.name}</Text>
</TouchableOpacity>
);
}
}
Obviously the second code snippet is much more cleaner.
Share Improve this question edited Jul 13, 2016 at 21:45 TimoStaudinger 42.6k16 gold badges89 silver badges96 bronze badges asked Jul 13, 2016 at 21:27 Joseph AnJoseph An 8221 gold badge7 silver badges19 bronze badges1 Answer
Reset to default 5You have to pass the onPress
prop through to the TouchableOpacity
:
class MenuButton extends Component {
render() {
return(
<TouchableOpacity onPress={this.props.onPress}>
<Text>{this.props.name}</Text>
</TouchableOpacity>
);
}
}
本文标签: javascriptReact Native onPress on component doesn39t functionStack Overflow
版权声明:本文标题:javascript - React Native: onPress on component doesn't function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745326181a2653602.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论