- React Native By Example
- Richard Kho
- 202字
- 2021-07-09 18:21:37
Saving updates
As we aren't using a navigation bar with the Android version of the app, we should create a Save button that handles the same save logic.
First, we should modify index.android.js to pass a saveCurrentEditedTask prop to EditTask from the TasksList component:
// index.android.js
...
class Tasks extends Component {
...
_renderScene (route, navigator) {
...
if (route.index === 1) {
return (
<EditTask
...
saveCurrentEditedTask={ route.passProps
.saveCurrentEditedTask }
...
/>
)
}
}
}
Then, modify TasksList to pass the _saveCurrentEditedTask method to EditTask in _renderAndroidEditTaskComponent:
// Tasks/app/components/TasksList/index.js
...
export default class TasksList extends Component {
...
_renderAndroidEditTaskComponent (rowID) {
this.props.navigator.push({
...
passProps: {
...
saveCurrentEditedTask: () =>
this._saveCurrentEditedTask(rowID),
...
}
})
}
...
}
After this, modify the Android version of EditTask to contain a new Button that calls its saveCurrentEditedTask when pressed:
// Tasks/app/components/EditTask/index.android.js
...
export default class EditTask extends Component {
static propTypes = {
...
saveCurrentEditedTask: PropTypes.func.isRequired,
...
}
render () {
...
return (
<View style={ styles.editTaskContainer }>
...
<View style={ styles.saveButton }>
<Button
color={ '#4E92B5' }
onPress={ () => this.props.saveCurrentEditedTask() }
title={ 'Save Task' }
/>
</View>
</View>
);
}
...
}
Finally, add some styling with a new saveButton property:
// Tasks/app/components/EditTask/styles.js
import { Navigator, StyleSheet } from 'react-native';
const styles = StyleSheet.create({
...
saveButton: {
flex: 1,
marginTop: 20,
maxHeight: 70,
},
...
});