1

Considering the following code:

import React from 'react';
import {strings as frstrings} from '../res/lang/fr/strings'
import {strings as engstrings} from '../res/lang/eng/strings'

class CurentLang {
     constructor(current){
        this.current = current;    
    }
    get current(){
        return this.current;
    }
    switchLang() {
        if(this.current === frstrings){
            this.current = engstrings;
        } else{
            this.current = frstrings;
        }
    }

}

var currentStrings = CurentLang(engstrings)

export default currentStrings;

I want the currentStrings object be the same across all other js files that will use it. However i am not exactly sure how since i can export one instance that will always contain engstrings but will not change universally if one js page decides to switch it up.

3
  • 1
    but will not change universally — what has led you to believe that's true? Commented Aug 30, 2020 at 20:33
  • well i am not sure how javascript handles exports of objects. It actually exports the same reference across all files? Commented Aug 30, 2020 at 20:35
  • 1
    Yes, the expectation is that if module A exports a single object, all other modules get a reference to the same one. Commented Aug 30, 2020 at 20:38

1 Answer 1

1

I would recommend you to look into React Context. This is the link to the documentation: https://reactjs.org/docs/context.html

Instead of using a class you could just use context instead and wrap your main App.js file with it. This is the first solution that comes to mind for me and is probably the most complete.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.