我环顾四周,试图用 React router 找到一个解决方案。通过 V5,您可以使用<Promt />.
我还尝试寻找一个普通的 JavaScript 解决方案,但没有任何效果。
我使用 React router v6 并histroy替换为const navigate = useNavigation()没有.listen属性的 router。
此外,v6 没有<Promt />组件。
尽管如此,最后我还是使用了useEffectclear函数。但这适用于组件的所有更改。前进的时候也是如此。
根据react.js文档,“React在组件卸载时执行清理”。
useEffect(() => {
// If user clicks the back button run function
return resetValues();;
})
Run Code Online (Sandbox Code Playgroud) 当方向发生变化时,请在图像的顶角使用“x”进行删除。我可以将垃圾桶图标和位置(img)的名称放在 img 旁边。学习如何在另一个顶角放置一个 'x'... 加上(也许)该地点的名称会很好。谢谢
...
class PlaceDetail extends Component {
state = {
viewMode: Dimensions.get('window').height > 500 ? 'portrait' : 'landscape'
}
constructor(props) {
super(props);
Dimensions.addEventListener('change', this.updateStyles )
}
componentWillUnmount() {
Dimensions.removeEventListener('change', this.updateStyles )
}
updateStyles = (dims) => {
this.setState({
viewMode: dims.window.height > 500 ? 'portrait' : 'landscape'
})
}
render(){
return (
<View style={styles.container}>
<View style={this.state.viewMode === 'landscape' ?
styles.viewImageLandScape : null } >
<Image
source={this.props.selectedPlace.image}
style={
this.state.viewMode === 'portrait' ?
styles.placeImagePortrait :
styles.placeImageLanscape }/>
<Text style={styles.placeName}>{this.props.selectedPlace.name}</Text> …Run Code Online (Sandbox Code Playgroud)