Ho avuto lo stesso problema con il navigatore di React Native che sono riuscito a risolvere utilizzando EventEmitters e Subscribables. Questo esempio è stato molto utile: https://colinramsay.co.uk/2015/07/04/react-native-eventemitters.html
Tutto quello che dovevo fare era aggiornare per ES6 e l'ultima versione di React Native.
livello superiore dell'app:
import React, { Component } from 'react';
import {AppRegistry} from 'react-native';
import {MyNavigator} from './components/MyNavigator';
import EventEmitter from 'EventEmitter';
import Subscribable from 'Subscribable';
class MyApp extends Component {
constructor(props) {
super(props);
}
componentWillMount() {
this.eventEmitter = new EventEmitter();
}
render() {
return (<MyNavigator events={this.eventEmitter}/>);
}
}
AppRegistry.registerComponent('MyApp',() => MyApp);
Nella funzione _renderScene del vostro navigatore, assicuratevi di includere il puntello "eventi":
_renderScene(route, navigator) {
var Component = route.component;
return (
<Component {...route.props} navigator={navigator} route={route} events={this.props.events} />
);
}
E qui è il codice per la FooScreen Componente che esegue il rendering di un elenco.
(Si noti che react-mixin è stato utilizzato qui per sottoscrivere l'evento Nella maggior parte dei casi mixins should be eschewed a favore di componenti di ordine superiore, ma non riuscivo a trovare un modo per aggirare in questo caso.):
import React, { Component } from 'react';
import {
StyleSheet,
View,
ListView,
Text
} from 'react-native';
import {ListItemForFoo} from './ListItemForFoo';
import reactMixin from 'react-mixin'
import Subscribable from 'Subscribable';
export class FooScreen extends Component {
constructor(props) {
super(props);
this._refreshData = this._refreshData.bind(this);
this._renderRow = this._renderRow.bind(this);
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds.cloneWithRows([])
}
}
componentDidMount(){
//This is the code that listens for a "FooSaved" event.
this.addListenerOn(this.props.events, 'FooSaved', this._refreshData);
this._refreshData();
}
_refreshData(){
this.setState({
dataSource: this.state.dataSource.cloneWithRows(//YOUR DATASOURCE GOES HERE)
})
}
_renderRow(rowData){
return <ListItemForFoo
foo={rowData}
navigator={this.props.navigator} />;
}
render(){
return(
<ListView
dataSource={this.state.dataSource}
renderRow={this._renderRow}
/>
)
}
}
reactMixin(FooScreen.prototype, Subscribable.Mixin);
Infine.Abbiamo bisogno di emettere in realtà che evento dopo il salvataggio di una Foo:
Nella tua NewFooForm.js componente si dovrebbe avere un metodo come questo:
_onPressButton(){
//Some code that saves your Foo
this.props.events.emit('FooSaved'); //emit the event
this.props.navigator.pop(); //Pop back to your ListView component
}
Hai accettato una risposta. Qualche possibilità che ti interessi di condividere un esempio di ciò che hai fatto per aggirare il problema? –