20
loading...
This website collects cookies to deliver better user experience
@media
rule. For this tutorial, we’ll focus on the Media query part of implementing RWD in web application.max-width
or min-width
values. These values determine on what screen sizes the content within them would be displayed or hidden....
const SampleComponent = (props) => {
...
return(
<div>
<ReactResponsive breakpoint={1040}>
<StyledComponents {...props}>
</ReactResponsive>
<ReactResponsive breakpoint={786}>
<StyledComponents {...props}>
</ReactResponsive>
</div>
)
}
...
min-width
of 1040px
, the children components, which is <StyledComponents {...props}>
would be render onto the screen. The same concept applies to the second <ReactResponsive breakpoint={786}>
component.import { useMediaQuery } from "react-responsive";
const App = () => {
const isDesktop = useMediaQuery({
query: "(min-width: 1224px)"
});
const isTablet = useMediaQuery({
query: "(max-width: 1224px)"
});
const isMobile = useMediaQuery({
query: "(max-width: 786px)"
});
const isPortrait = useMediaQuery({
query: "(orientation: portrait)"
});
const isRetina = useMediaQuery({
query: "(max-resolution: 300dpi)"
});
return (
<Wrapper>
{
isDesktop ? <DesktopView /> : <MobileView />
}
{isTablet && <Tablet />}
<p>This is {isPortrait ? 'portrait' : 'landscape'} orientation</p>
{isRetina && <p>You are testig retina</p>}
</div>
</Wrapper>
)
}
useMediaQuery
hook provided by react-responsive, we were able to created breakpoint logic within our code. Components validated by isDesktop
will display only on screens above the viewport of 1224px
. While isTablet
and isMobile
will display components within the viewport of 1224px
and 786px
respectively.import MediaQuery from "react-responsive";
const App = () => (
<Wrapper>
<MediaQuery minWidth={1224}>
<DeskTopView>This will display in desktop screen</DeskTopView>
<MediaQuery minWidth={1824}>
<Banner text="this will dsiplay on larger screen size" />
</MediaQuery>
</MediaQuery>
<MediaQuery minResolution="2dppx">
{(matches) =>
matches
? <Retina>This is a retina view</Retina>
: <div>This isn't a retina</div>
}
</MediaQuery>
</Wrapper>
)
MediaQuery
component, and then pass in the breakpoint as a prop into the component. Also within the <MediaQuery minResolution="2dppx">
, we used a function (render prop) as a child to programmatically choose the children props of the component.onChange
prop in the MediaQuery
component or as a callback in our useMediaQuery
hook.import MediaQuery, { useMediaQuery } from "react-responsive";
const App = () => {
const handleMediaQueryChange = (matches) => {
if(matches) {
// do something if true
} else {
// do something if false
}
};
const isDesktop = useMediaQuery(
{ minWidth: 1224 }, undefined, handleMediaQueryChange
);
return (
<Wrapper>
<MediaQuery minWidth={1224} onChange={handleMediaQueryChange}>
...
</MediaQuery>
{"or"}
{isDesktop && <div>Welcome to desktop view...</div>}
</Wrapper>
)
}
handleMediaQueryChange
, to the useMediaQuery
hook to handle onChange
event that occurs when the browser’s viewport changes to 1224
px in width. This also applies to the MediaQuery
component, where the handleMediaQueryChange
function is executed by the onChange
prop on the component.dependencies
and the devDependencies
needed.git clone https://github.com/IkehAkinyemi/react-responsive-portfolio portfolio
cd portfolio
yarn install
yarn start
command in the terminal, and on the browser we navigate to the route, localhost:3000/experience
to view the current UI before implementing our media query.import Projects from "./Projects";
import MediaQuery from "react-responsive";
...
const Experience = () => {
...
return (
<div className="container max-w-screen-lg mx-auto">
<div className="mt-8">
<MediaQuery maxWidth={786}>
<h1 className="text-3xl text-center mb-6 z-50">
For desktop view only
</h1>
</MediaQuery>
<MediaQuery minWidth={1224}>
<Projects />
</MediaQuery>
</div>
</div>
);
};
export default Experience;
h1
element in mobile view, while the list of cards for the projects, i.e the <Project />
component, displays only on viewport above 1224px
. Card
component in the src/experience/Project.tsx
file. Here we’ll make the content of the custom component, Glass
, only visible within desktop viewport.import { useMediaQuery } from "react-responsive";
...
const Card = ({ img, name, description }: CProps) => {
const isDesktop = useMediaQuery({
query: "(min-width: 1224px)"
});
return (
<Wrapper bg={img} className="SpinWhot project">
<div className="project-name-holder">
<p className="project-name-holder-SpinWhot project-title">{name}</p>
</div>
<Glass className="">{isDesktop && description}</Glass>
</Wrapper>
);
};
...
onChange
event within our application to always trigger when we enter a particular viewport.import { useMediaQuery } from "react-responsive";
...
const Card = ({ img, name, description }: CProps) => {
const handleMediaQueryChange = (matches: boolean) => {
if(matches) {
window.alert("Trigger a window's alert at viewport of 1244px")
}
};
const isDesktop = useMediaQuery(
{ minWidth: 1224 }, undefined, handleMediaQueryChange
);
return (
<Wrapper bg={img} className="SpinWhot project">
<div className="project-name-holder">
<p className="project-name-holder-SpinWhot project-title">{name}</p>
</div>
<Glass className="">{isDesktop && description}</Glass>
</Wrapper>
);
};
...
window.alert
function to display an alert on the browser whenever we resizes the width of our browser to 1224px
in value.