I'm developing a simple todo-list app using React Native, my issue is the following: I have a NavigatorIOS at the root of my project, with a component containing a ListView as initial route, and a navigation bar button that leads to a task creation view.
Once a new task has been created, the view is pop so that the ListView is displayed. I'm trying to add my newly created task to this ListView (its data source is contained in the component state).
How to perform such an operation, what's the good practice? I would use a delegate in the pure native app but here, both views are handled by the NavigatorIOS instance.
index.ios.js
addTask() {
console.log("Test");
},
render() {
return (
<React.NavigatorIOS
ref="nav"
style={styles.container}
tintColor="#ED6063"
initialRoute={{
title: "Tasks",
component: TasksList,
rightButtonTitle: 'Add',
onRightButtonPress: () => {
this.refs.nav.navigator.push({
title: "New task",
component: NewTask,
passProps: {
onTaskAdded: this.addTask
},
leftButtonTitle: "Cancel"
});
}
}}/>
);
}
NewTask.js
taskAdded() {
console.log("Added: " + this.state.title + " - " + this.state.description);
this.props.onTaskAdded({
title: this.state.title,
description: this.state.description
});
this.props.navigator.pop();
}
TasksList.js
var dataSource = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2
});
this.state = {
dataSource: dataSource.cloneWithRows(data)
};
You can find the complete source code here.
You should rewrite your
constructorfunction to get the data from a dynamic way. Then when the page reloads, it would get a correct data which includes the new task. Here you get the data from a static array, which would not change.Save tasks list to local file or Firebase, and read when construct.