Javascript 是否有方法使用React Native创建和分派/触发自定义事件?
使用DOM,您可以使用Javascript轻松创建触发器自定义事件,如下所示:Javascript 是否有方法使用React Native创建和分派/触发自定义事件?,javascript,react-native,events,triggers,dispatch,Javascript,React Native,Events,Triggers,Dispatch,使用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做到这一点?我知道Native
var event = new Event('build');
// Listen for the event.
elem.addEventListener('build', function (e) { /* ... */ }, false);
// Dispatch the event.
elem.dispatchEvent(event);
有没有办法用React Native做到这一点?我知道NativeEventMitter的存在,它是RN用Javascript与本机平台进行通信的一种方式。对于我的情况,实际上有两种解决方案:
使用react导航了解组件/屏幕何时收到 使用“didFocus”事件侦听器聚焦并触发操作:
import React,{Component}来自'React';
从“react native”导入{View};
从“react navigation”导入{withNavigation};
类TabScreen扩展组件{
componentDidMount(){
const{navigation}=this.props;
this.focusListener=navigation.addListener('didFocus',()=>{
//屏幕聚焦
//采取行动
});
}
组件将卸载(){
//删除事件侦听器
this.focusListener.remove();
}
render(){
返回;
}
}
使用导航导出默认值(选项卡屏幕);
将Redux用作应用程序的一个全局状态容器
你可以试试图书馆。这可能会有帮助。你想通过这些活动实现什么?谢谢你的建议,我会尝试一下,并让你知道@ChristopherBradshaw@JRK我已经为
AsynchStorage
创建了一个名为deviceStorage
的包装器,其中,在我的字典应用程序中,用户可以使用特定键将单词添加到保存在deviceStorage
中的收藏夹列表/数组中。我想知道何时有一个新词被添加到收藏夹
列表中,这样我就可以将此更新反映到主页上,而主页实际上是应用程序的上一个屏幕componentDidMount
将无法工作,因为主页已装入(仍在屏幕堆栈中)。PS:您在收藏夹列表中实际添加新词的屏幕将从主屏幕推出。我不介意分享你的代码。Thx@Monero你找到解决办法了吗?
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);