React navigation goBack() and set parent state

Amin

I have a page. Screen as below

import React, { useState } from "react";
import { Button, Text, View } from "react-native";
export default function ViewA() {
     const [val, setVal] = useState('')
     return (
          <View>
               <Text>{val}</Text>
               <Button title="Next" onPress={()=>navigation.navigate(?)} />
          </View>
     );
}

and another page. Screen as below :

import React from "react";
import { Button } from "react-native";
export default function ViewB() {
     return <Button title="back" onPress={()=>navigation.goBack(?) } />;
}

I have two Screen viewA and viewB. in viewA a have a button. when the button press navigate to viewB and pass the setVal() function as param. then in Screen viewB when back button tap setVal() call with new value and navigation.goBack() call to come back to viewA

Hamas Hassan

You have to pass a callback function as a param from screen A to B and when button press in screen B pass your new value you like to set in the sceen A.

Complete Code:

Screen A

import React, { useState } from "react";
import { Text, View, Button } from "react-native";

const ViewA = ({ navigation }) => {
  const [val, setVal] = useState(null);

  const callBack = (value) => {
    setVal(value);
  };

  const onNextPress = () => {
    navigation.navigate("Second Screen", { callBack: callBack });
  };

  return (
    <View>
      <Text>{val}</Text>
      <Button title="Next" onPress={onNextPress} />
    </View>
  );
};

export default ViewA;

Screen B

import React from "react";
import { View, Button } from "react-native";

const ViewB = ({ route, navigation }) => {
  const onBackPress = () => {
    const { callBack } = route.params;
    callBack(5); // Your new value to set
    navigation.goBack();
  };

  return (
    <View>
      <Button title="back" onPress={onBackPress} />
    </View>
  );
};

export default ViewB;

I hope this helps you out!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

React navigation goBack() and update parent state

From Dev

Manage Redux state with React navigation goBack() function

From Java

React hooks - set state of Parent from child via function

From Dev

Giving a state to parent component in React

From Dev

React State not available from Parent?

From Dev

React - redux or local state for different navigation levels

From Dev

React native navigation how to connect it to Redux state?

From Dev

React set state property dynamically

From Dev

Not able to set state in componentWillReceiveProps in React

From Dev

React set state property dynamically

From Dev

Not able to set state in componentWillReceiveProps in React

From Dev

Unable to set State of React Component

From Dev

Loop through react to set state

From Dev

Notifying when React state is set?

From Dev

Set state on specific component in React

From Dev

React - Form submit + set state

From Dev

React setState does not set the state

From Java

React Child with useEffect() not updating on Parent State Change

From Java

How to update parent's state in React?

From Java

React Parent/Child State Change Design Question

From Java

Change react hook state from parent component

From Dev

Refreshing children state from parent React

From Dev

React is sending old state to its parent

From Dev

React is sending old state to its parent

From Dev

Loading state from parent route component in react

From Dev

Is it ok to store React component in parent's state?

From Dev

How to get state from child to parent react

From Dev

Add goBack and goForward to the right of the Navigation Bar

From Dev

componentDidMount not being called after goBack navigation call

Related Related

HotTag

Archive