- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不知道为什么当我触发事件时 DeviceEventEmitter.addListener 它已经发出一次但听了两次。
我有一个组件 底座 它已添加到每个屏幕中,例如
<View>
{this.props.children}
<ModalComponent />
</View>
constructor (props) {
super(props)
this.state = {
modalVisible: false
}
}
componentDidMount() {
DeviceEventEmitter.addListener('ModalVisible', this.onModalVisible)
}
componentWillUnmount () {
DeviceEventEmitter.removeListener('ModalVisible', this.onModalVisible)
}
onModalVisible = (args) => {
console.log(['ModalVisible', args]) // logging twice
this.setState({
modalVisible: args.visible
})
}
close () {
this.setState({
modalVisible: false
})
}
onRequestClose = () => {
this.close()
}
render() {
return (
<Modal animationType={'slide'} transparent={false} visible={this.state.modalVisible} onRequestClose={this.onRequestClose}>
...
</Modal>
)
}
static show (data) {
console.log(['Service.show', data]) // only once
DeviceEventEmitter.emit('ModalVisible', { visible: true })
}
this.listener = DeviceEventEmitter.addListener(...)
this.listener.remove()
this.onModalVisible.bind(this)
最佳答案
我在使用 socket.io 触发/注册两次事件时遇到了同样的问题,我的问题是由于我在 DidMount 方法上添加了 eventListeners。但是由于我的组件安装了多个,因此它也多次添加了 eventListeners。
我的猜测是您多次使用相同的组件,因此多次添加相同的 eventListener。尝试将您的 eventsListener 添加到另一个只会被调用一次的地方。
关于react-native - DeviceEventEmitter 它已经听了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47436466/
我不知道为什么当我触发事件时 DeviceEventEmitter.addListener 它已经发出一次但听了两次。 我有一个组件 底座 它已添加到每个屏幕中,例如 {this.props
我想使用事件在 native ios/android 和我的 native 应用程序之间进行通信。 我看到了两种方法来做到这一点:DeviceEventEmitter 和 NativeAppEvent
我正在使用 DeviceEventEmitter 来处理最喜欢的方法的事件,该方法已在构造函数中订阅: DeviceEventEmitter.addListener("FavoriteClick",
升级到0.26.0-rc版本后,在iOs上这一行: DeviceEventEmitter.addListener('keyboardWillShow', (e)=>this.updateKeyboar
我是一名优秀的程序员,十分优秀!