npm i react-use-media
App.jsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import React from 'react'; import { useMedia } from 'react-use-media'; const App = () => { const isWide = useMedia({ minWidth: 800 }); return ( <div style={{ display: 'flex', minHeight: '100vh' }}> {isWide ? ( // Desktop View with Sidebar <> <aside style={{ width: '250px', backgroundColor: '#f0f0f0', padding: '1rem' }}> <h3>Sidebar</h3> <ul> <li>Dashboard</li> <li>Profile</li> <li>Settings</li> </ul> </aside> <main style={{ flex: 1, padding: '1rem' }}> <h2>Welcome to the Desktop View</h2> <p>This layout includes a sidebar because your screen is wide.</p> </main> </> ) : ( // Mobile View with Toggle Menu <main style={{ padding: '1rem' }}> <h2>Welcome to Mobile View</h2> <button onClick={() => alert('Show mobile menu')} style={{ padding: '0.5rem 1rem', marginBottom: '1rem' }} > ☰ Menu </button> <p>This is a simplified view for smaller screens without a sidebar.</p> </main> )} </div> ); }; export default App; |