-1

**> I have a function to dynamically change the language of the page but

only the one of the "navbar" component is changed, for that I have created a global variable but the default value is not changed.**

  • 1
    You'll need to show you're attempt or this will quickly become closed – andy mccullough Jun 03 '22 at 10:11
  • Welcome to Stack Overflow! Nobody can tell you what's wrong with code we can't see. Please provide a [mcve] demonstrating the problem. To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Jun 03 '22 at 10:11
  • There are multiple ways to do it. Like putting the language variable on localstorage, and cookies for long time storage of the value. Else you can use Database to always keep this information stored for a user. For a global value you can also use `window` object or can try one of the methods suggested in https://stackoverflow.com/a/51240514/3855179 and https://stackoverflow.com/questions/34351804/how-to-declare-a-global-variable-in-react – Sumit Surana Jun 03 '22 at 10:18
  • It all depends on what way you are trying to save it and what scope it has. So a minimal reproducible code would help in getting a right suggestion – Sumit Surana Jun 03 '22 at 10:19

1 Answers1

0

You should never use plain global variables in React app as their change will not trigger rerender of the component tree. If you need a variable accessible globally in your app use Context or some state management library like Jotai or in more complex cases Redux.

Droow
  • 416
  • 2
  • 10