In our component we will listen for this event, from the window object by calling window.matchMedia
useEffect(()=>{letmediaQuery=window.matchMedia("(min-width: 768px)");mediaQuery.addListener(setMQuery);// this is the cleanup function to remove the listenerreturn ()=>mediaQuery.removeListener(setMQuery);},[]);
the addListener calls our setState function to hold the results, and the changing of the state variable will cause the component to rerender.
Based on the state variable we will render the hamburger menu or the list of buttons that correspond to the side menu items
Full source for NavButtons component
// NavButtons.tsxexportconstNavButtons=()=>{const[mQuery,setMQuery]=React.useState<any>({matches:window.innerWidth>768?true:false,});useEffect(()=>{letmediaQuery=window.matchMedia("(min-width: 768px)");mediaQuery.addListener(setMQuery);// this is the cleanup function to remove the listenerreturn ()=>mediaQuery.removeListener(setMQuery);},[]);// MediaQueryListEvent { isTrusted: true, media: "(min-width: 768px)", matches: true ...}return (<div>{mQuery&&!mQuery.matches?(<IonMenuButton/>):(<><IonButtonrouterLink={"/home"}>Home </IonButton><IonButtonrouterLink={"/page-1"}>One </IonButton><IonButtonrouterLink={"/page-2"}>Two</IonButton></>)}</div>);};
Then we use the component in the IonToolbar of our pages, see an example below