React Native, Edit Data From Another Screen
I made 2 screens one home screen and 2nd edit screen I need to edit data of home screen from edit screen and save it and that data should also update in home and detail screen. How
Solution 1:
You can pass a function from your home screen to setState for it in your edit screen. In case navigating to edit screen causes the home screen unmounted, you can change the navigate method to push of stack navigator (I haven't tested yet). The code now should look like:
HomeScreen.js:
onEdit=(data)=>{
setState(...);
}
...
<TouchableOpacity
activeOpacity={0.7}
onPress={() =>this.props.navigation.navigate("Edit", {item, onEdit})} //use push instead if error occured
style={styles.Edit}
>
...
Edit.js
classListDetailsextendsComponent {
render() {
const {item:listing, onEdit} = this.props.route.params;
return (
<View><Imagestyle={styles.image}source={listing.image} /><Viewstyle={styles.detailContainer}><AppTextInputvalue={listing.title} /><AppTextInputvalue={listing.des} /></View><AppButtontext="Save"onPress={() => { onEdit(...); this.props.navigation.goBack("Home");}}
/>
</View>
Post a Comment for "React Native, Edit Data From Another Screen"