使用DOM,您可以像这样轻松地使用Javascript创建触发器自定义事件:
var event = new Event('build');
// Listen for the event.
elem.addEventListener('build', function (e) { /* ... */ }, false);
// Dispatch the event.
elem.dispatchEvent(event);
有没有办法用React-Native做到这一点?我知道NativeEventEmitter的存在,这是RN与Java平台与本机通信的方式。
答案 0 :(得分:0)
针对我的情况,实际上有两种解决方案:
import React, { Component } from 'react';
import { View } from 'react-native';
import { withNavigation } from 'react-navigation';
class TabScreen extends Component {
componentDidMount() {
const { navigation } = this.props;
this.focusListener = navigation.addListener('didFocus', () => {
// The screen is focused
// Call any action
});
}
componentWillUnmount() {
// Remove the event listener
this.focusListener.remove();
}
render() {
return <View />;
}
}
export default withNavigation(TabScreen);