react check if browser is in dark mode
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
// dark mode
}
//To watch for changes:
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
const newColorScheme = e.matches ? "dark" : "light";
});
//Or in React Hooks:
const [mode, setMode] = useState<"light" | "dark" | undefined>(
window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? "dark" :"light"
);
useEffect(() => {
const modeMe = (e: any) => {
setMode(e.matches ? "dark" : "light");
}
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', modeMe);
return window.matchMedia('(prefers-color-scheme: dark)').removeListener(modeMe);
}, []);
Also in JavaScript:
- async axios call
- open folder node js
- node js callback
- jquery on click remove parent div
- getting average of array javascript
- get all parent nodes of child in javascript array
- JavaScript Operators
- javascript array add front
- javascript string keys
- Arranging Coins
- javascript how to remove first element of array
- console.log clear screen
- Javascript remove all child elements
- añadir react a node project
- npm react router dom
- jquery for bootstrap 4
- h«javascript variable declaration
- break string to array javascript without delimeter
- array con tridimensional javascript
- isDate javascript
- add items to a list in a document monoose
- filter array with unique objects javascript
- promise catch
- glitch node version