skip to Main Content

In my React-Native App; I have one const variable declared as below in one component named: constants.js

export const IS_VIA_DEEP_LINK = false

Now,

In my Splashscreen.js I am doing as below:

constants.IS_VIA_DEEP_LINK=true;

When I try to access the value of IS_VIA_DEEP_LINK in the same Splashscreen.js or any other functional component I get the updated value- true.

How is it possible?

2

Answers


  1. The simplest way is use react-native-async-storage:

    1. Storing data:
    const storeData = async (value) => {
      try {
        await AsyncStorage.setItem('IS_VIA_DEEP_LINK', value);
      } catch (e) {
        // saving error
      }
    };
    
    1. Reading data:
    const getData = async () => {
      try {
        const value = await AsyncStorage.getItem('IS_VIA_DEEP_LINK');
        if (value !== null) {
          // value previously stored
        }
      } catch (e) {
        // error reading value
      }
    };
    

    You can use 2 functions above in anywhere of your project

    Login or Signup to reply.
  2. You cannot change the value of a primitive constant variable. Primitive types in JavaScript are string, number, bigint, boolean, undefined, symbol, and null.

    1. Method

    Define the IS_VIA_DEEP_LINK variable as let.

    export let IS_VIA_DEEP_LINK = false
    
    2. Method

    If you want to keep your variable as a constant put it in a object and export that object. Since objects are stored by reference, you can change their values even if the object defined as a constant.

    export const constants = {
      IS_VIA_DEEP_LINK: false
    }
    
    constants.IS_VIA_DEEP_LINK = true
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search