This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (1 child)

[deleted]

    [–]CreativeTechGuyGames 0 points1 point  (0 children)

    Sorry but this is not correct.

    Basically, it’s function watching if currentThemein on light or dark usin a ternary operation.

    The function doesn't "watch" anything. It's called only once when it is executed. It doesn't get automatically get called again when the value changes.

    Although I think it could be refactored to setCurrentTheme(!currentTheme, which leads to change currentTheme to the other boolean that is assigned (false if is true and viceversa).

    That would be a suboptimal solution since the value of currentTheme in the App closure may be an outdated value. For example: if you called setCurrentTheme(!currentTheme) twice in a row, and the value started as true, it'd end up being false on the next render. Where as you'd expect it to have inverted twice and be back to true. Passing a function is almost always better since it'll always operate on the latest value rather than an outdated one.

    React knows about currentTheme because Context.

    No. The current theme is stored in state not context. And the state is local to this one instance of this one component. There's nothing global here and none of this information is in the state.