Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communicate Between Components in React Native?

Tags:

react-native

Here is my react-native code:

<NavigatorIOS
ref='nav'
style={styles.container}
initialRoute={{
    title: 'List',
    component: TaskList,
    rightButtonTitle: 'new',
    onRightButtonPress: () => {
        this.refs.nav.push({ 
        title: 'Add Task',
        component: AddTask
        });
    }
}} />

How can I push some notifications from the AddTask component to the TaskList component?

The docs said that "For communication between two components that don't have a parent-child relationship, you can set up your own global event system."

So I need set up a global event system?

like image 711
backslash112 Avatar asked Dec 24 '15 06:12

backslash112


People also ask

How do you communicate between independent components in React Native?

To communicate between two independent components in React, you have the flexibility to set up a global event-driven system, or a PubSub system. An event bus implements the PubSub pattern and allows you to listen and dispatch events from components.

How do components communicate with each other?

So we need to understand how components communicate with each other. There are three ways: parent to child - sharing data via input. child to parent - sharing data via viewChild with AfterViewInit.


1 Answers

Like communicate-between-components said:

For communication between two components that don't have a parent-child relationship, Flux pattern is one of the possible ways to arrange this.

You can use Flux in the react-native project.

Here is a demo for Flux + React-Native:

You can check out the source code from github.

like image 158
backslash112 Avatar answered Oct 02 '22 16:10

backslash112