2

I'm new to NextJS, and trying to figure out, how to create a global variable that I could assign a different value anytime. Could someone give a simple example? (I know global might not be the best approach, but still I would like to know how to set up a global variable).

Let's say:

_app.js

NAME = "Ana" // GLOBAL VARIABLE

page_A.js

console.log(NAME) // "Ana"
NAME = "Ben"

page_B.js

console.log(NAME) // "Ben"
Isaac Michaan
  • 313
  • 1
  • 9
  • Does this answer your question? [How to declare a global variable in React?](https://stackoverflow.com/questions/34351804/how-to-declare-a-global-variable-in-react) – juliomalves Feb 13 '22 at 17:06
  • 1
    No, but this does: https://www.netlify.com/blog/2020/12/01/using-react-context-for-state-management-in-next.js/ and this does: https://reacttricks.com/sharing-global-data-in-next-with-custom-app-and-usecontext-hook/ and also this: https://www.linkedin.com/pulse/react-context-api-nextjs-axel-laurent-obscura-sarzotti thanks anyways – Isaac Michaan Feb 13 '22 at 19:12

1 Answers1

0

Nextjs no special ways to provide global variables you want. You can achieve by:

  1. Stateful management tool, like redux-react
  2. Using Context
Pandy
  • 19
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 20 '22 at 11:49