Tanveerbyn
Tanveerbyn

Reputation: 784

Update Data from one to. another screen

hey guys I'm using a text field that can show my global variable value when I update global variable ex:- global.xx++ it can't refresh when I click on it .how do I update data at once from many screen.. here's my ex

class A extend component
{
if(responseJson.responseCode == '200'){
                          ++global.cartitems ;
                          Obj.onPress();
                            Obj.componentDidMount();
                        Alert.alert('SHOPPING','Item added successfully.',[{text: 'OK',},],{ cancelable: false })

                        }

class b extend component
{
  <Text
             style={{color:'white',textAlign:'center',fontSize:10}}

             >{global.cartitems}</Text>
}

Upvotes: 1

Views: 492

Answers (1)

Jason Hong
Jason Hong

Reputation: 79

I believe what you meant is to pass a value from one screen to another.

You can either use AsyncStorage to keep the data inside your memory, or pass the data through props navigation

AsyncStorage example:

Store Data:

_storeData = async () => {
  try {
    await AsyncStorage.setItem('@MySuperStore:key', 'I like to save it.');
  } catch (error) {
    // Error saving data
  }
}

Fetch Data:

_retrieveData = async () => {
  try {
    const value = await AsyncStorage.getItem('TASKS');
    if (value !== null) {
      // We have data!!
      console.log(value);
    }
   } catch (error) {
     // Error retrieving data
   }
}

Passing data through props navigation:

First class:

<Button onPress = {
  () => navigate("ScreenName", {name:'Jane'})
} />

Second class:

const {params} = this.props.navigation.state

Upvotes: 1

Related Questions