Skip to content Skip to sidebar Skip to footer

Is There A Way To Create And Dispatch/trigger Custom Event With React-native?

With DOM, you can easily create a trigger custom event with Javascript like so: var event = new Event('build'); // Listen for the event. elem.addEventListener('build', function (

Solution 1:

There are actually two solutions for my case:

  1. https://reactnavigation.org/docs/en/function-after-focusing-screen.html
    Use react-navigation to know when the component/screen has received focus and trigger an action with a 'didFocus' event listener:
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);
  1. https://redux.js.org/basics/example
    Use Redux as one global state container for your application.

Post a Comment for "Is There A Way To Create And Dispatch/trigger Custom Event With React-native?"