JavaScript React Basics (retired) Designing Data Flow Building the Statistics Component

Gordon Freeman
PRO
Gordon Freeman
Pro Student 930 Points

Why can't we just use global variables Instead of State? then I think we don't need to pass all the data around.

If I just use global variable or object which contains players and counter then wouldn't it be much easier for them(components) to just access it without passing arguments? There must be a good answer for this. please enlighten me!

2 Answers

Ignazio Calo
PRO
Ignazio Calo
Pro Student 1,819 Points

you're facing a true problem when it comes to design the architecture of an app. At the first look you think that a global variable would make the app way more easy, but i you google a bit you'll see that everyone says "please do not use global variables".

There are some "official approved" explanations why global variables are bad here: http://wiki.c2.com/?GlobalVariablesAreBad Additionally with JS you can have some additional troubles caused by the language itself (performances and overriding).

In few words, in a small app a global variable is easier to use but as soon the app grows it's hard to keep track of all the pieces of the app that edit the variable making code less easy to read/maintain

Jesse Thompson
Jesse Thompson
10,684 Points

I kind of wondered this too and Im glad this question was asked. Thanks Ignazio for the response.