- 39
- 0
我的代码是这样的:
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, View, TouchableHighlight, Text } from 'react-native';
export default class LearnRN extends Component {
constructor(props){
super(props);
this.state = {
text:'',
}
this.press = this.press.bind(this);
}
press(){
this.setState({text:'hello'});
}
render(){
return (
<View style={styles.container}>
<TouchableHighlight onHideUnderlay={this.press}>
<View style={styles.button}/>
</TouchableHighlight>
<Text>{this.state.text}</Text>
</View>
);
}
}
补充:onShowUnderlay也无法触发,但是onPress没有问题,这是为什么?
- 共 0 条
- 全部回答
-
麻辣章鱼 普通会员 1楼
在React Native中,组件的生命周期方法包括:componentDidMount(),componentWillUnmount(),componentDidUpdate(),componentWillUnmount(),componentDidUpdate(),componentWillUnmount(),componentDidMount(),componentWillUnmount()等。当组件从视图上卸载时,对应的生命周期方法会被调用,而onHideUnderlay这个生命周期方法在组件卸载之前不会被调用。
因此,你可能需要在onHideUnderlay这个生命周期方法中添加一些处理代码,例如检查当前是否是在视图上卸载之前,如果是的话,就取消点亮高亮。
以下是一个简单的示例:
```javascript import React, { Component } from 'react'; import { TouchableOpacity, StyleSheet, Text } from 'react-native';
class Highlight extends Component { constructor(props) { super(props); this.state = { isUnderlayVisible: false, }; }
toggleUnderlay() { this.setState(prevState => ({ isUnderlayVisible: !prevState.isUnderlayVisible, })); }
componentDidMount() { this.toggleUnderlay(); }
componentWillUnmount() { this.toggleUnderlay(); }
render() { return (
this.toggleUnderlay()} style={styles.button} > ); } }点击以取消高亮 const styles = StyleSheet.create({ button: { backgroundColor: '#333', alignItems: 'center', justifyContent: 'center', padding: 10, borderRadius: 5, width: 200, height: 50, }, text: { color: '#fff', fontSize: 16, fontWeight: 'bold', textAlign: 'center', }, });
export default Highlight; ```
在这个示例中,我们首先在componentDidMount()方法中调用toggleUnderlay()方法,然后在componentWillUnmount()方法中调用toggleUnderlay()方法。这样,每次组件从视图上卸载时,都会调用这两个方法,从而确保在视图上卸载之前,高亮不会被取消。
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部

