How to add a dark mode to your React web app Wachira on August 16, 2019 We are currently living in a dark mode in everything era. Most apps nowadays come with a switch either at the navbar or just somewhere around its s... Read full post Submit Preview Dismiss Collapse Expand Chris Achard Chris Achard Chris Achard Follow I'm trying to teach everything I know at chrisachard.com Instructor at egghead.io Mostly, I use JS, React, Rails, and Node Email chris@nanohop.com Location USA Work I teach people stuff! at Myself :) I'm an independent consultant - mostly javascript Joined Aug 14, 2019 • Aug 16 '19 Dropdown menu Copy link Hide Very nice! Great idea storing it in localstorage so it's the same when the user comes back the next time. Also love the variables in CSS - I definitely didn't know that those were supported outside of scss, etc! Collapse Expand Wachira Wachira Wachira Follow 21 | Dev Email email@bywachira.com Location Any where software takes me Joined Feb 11, 2019 • Aug 16 '19 Dropdown menu Copy link Hide Yes if you close that tab and come visit the link again it will still be the theme you last set Collapse Expand Andrés Pérez Andrés Pérez Andrés Pérez Follow Web developer from 🇨🇴 Location Colombia Work Fullstack Developer at Yuxi Global Joined Dec 24, 2017 • Aug 16 '19 • Edited on Aug 16 • Edited Dropdown menu Copy link Hide Pst, I gave it a spin and implemented it using hooks, here's the comparison gist.github.com/Oxyrus/b4edab0372b... (doesn't include the fancy toggle because I was lazy 😆) Collapse Expand Thomas Ghysels Thomas Ghysels Thomas Ghysels Follow JS all the way Location Ghent, Belgium Work Fullstack at Freelance Joined Jan 10, 2019 • Aug 18 '19 Dropdown menu Copy link Hide Cool, I would have used useLocalState from the use-hooks package. Collapse Expand Wachira Wachira Wachira Follow 21 | Dev Email email@bywachira.com Location Any where software takes me Joined Feb 11, 2019 • Aug 17 '19 Dropdown menu Copy link Hide Nice 💯 Collapse Expand arnu515 arnu515 arnu515 Follow Joined Jun 10, 2020 • Jun 15 '20 Dropdown menu Copy link Hide You're writing longer, more complicated code. For example, instead of writing checked: localStorage.getItem("theme") === "dark" ? true : false, You can write: checked: localStorage.getItem("theme") === "dark", because localStorage.getItem("theme") === "dark" is a boolean and its value will be true or false. Also, instead of writing document.getElementsByTagName("HTML")[0] You can write document.querySelector("html") and it will do the same thing. There's nothing wrong with your code, but I'm just telling you how to make it more efficient Collapse Expand Andrés Pérez Andrés Pérez Andrés Pérez Follow Web developer from 🇨🇴 Location Colombia Work Fullstack Developer at Yuxi Global Joined Dec 24, 2017 • Aug 16 '19 Dropdown menu Copy link Hide Question, what's the point of having a theme property inside your state? I don't see you using it anywhere. Collapse Expand Wachira Wachira Wachira Follow 21 | Dev Email email@bywachira.com Location Any where software takes me Joined Feb 11, 2019 • Aug 17 '19 Dropdown menu Copy link Hide I kinda forgot to remove it. Before I was using the theme value to toggle the theme change Collapse Expand Aneudys Amparo Aneudys Amparo Aneudys Amparo Follow Entrepreneur & Hustler Self-taught Software Developer Location Dominican Republic Work Software Developer at In Finance Industry Joined Apr 10, 2020 • Apr 11 '20 Dropdown menu Copy link Hide WOW 🤩 Simple and directly to the point! Thank you for posting, I’ll use your guide as starting point. Collapse Expand Comment deleted Collapse Expand Wachira Wachira Wachira Follow 21 | Dev Email email@bywachira.com Location Any where software takes me Joined Feb 11, 2019 • Aug 16 '19 Dropdown menu Copy link Hide Anytime Code of Conduct • Report abuse
Very nice! Great idea storing it in localstorage so it's the same when the user comes back the next time.
Also love the variables in CSS - I definitely didn't know that those were supported outside of scss, etc!
Yes if you close that tab and come visit the link again it will still be the theme you last set
Pst, I gave it a spin and implemented it using hooks, here's the comparison gist.github.com/Oxyrus/b4edab0372b... (doesn't include the fancy toggle because I was lazy 😆)
Cool, I would have used useLocalState from the use-hooks package.
Nice 💯
You're writing longer, more complicated code. For example, instead of writing
You can write:
because
localStorage.getItem("theme") === "dark"is a boolean and its value will be true or false.Also, instead of writing
You can write
and it will do the same thing.
There's nothing wrong with your code, but I'm just telling you how to make it more efficient
Question, what's the point of having a
themeproperty inside your state? I don't see you using it anywhere.I kinda forgot to remove it. Before I was using the theme value to toggle the theme change
WOW 🤩 Simple and directly to the point!
Thank you for posting, I’ll use your guide as starting point.
Anytime