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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With