Skip to content

Commit

Permalink
useMedia - initialize state with call to media query
Browse files Browse the repository at this point in the history
Initializing state with result from call to window.matchMedia avoids possible initial flicker of returned value, when media query matched initially
  • Loading branch information
mattleonowicz authored Jul 11, 2019
1 parent c4f5f16 commit eeb55e4
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/useMedia.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useState } from 'react';

const useMedia = (query: string, defaultState: boolean = false) => {
const [state, setState] = useState(defaultState);
const useMedia = (query: string) => {
const [state, setState] = useState(() => window.matchMedia(query).matches);

useEffect(() => {
let mounted = true;
Expand Down

0 comments on commit eeb55e4

Please sign in to comment.