Install
yarn add react-sizes
npm install react-sizes
What and why
React Sizes is a high-order component with a high performance that transform window sizes (width and height) into props.
You can check inside your component, for example, if user's window is less than 480 pixels of width, and add a custom
content.
It can be very powerful for when you need to display different content for mobile and desktop. But it's not limited to this case. Just use that at your needs.
Usage
With class component.
import React Component from 'react'import withSizes from 'react-sizes' { return <div>thispropsisMobile ? 'Is Mobile' : 'Is Not Mobile'</div> } const mapSizesToProps = width isMobile: width < 480 mapSizesToPropsMyComponent
You can play with this example here.
As decorator.
import React from 'react'import withSizes from 'react-sizes' @ { return <div>thispropsisMobile ? 'Is Mobile' : 'Is Not Mobile'</div> }
Interoperate with other libraries.
import React from 'react'import withSizes from 'react-sizes'import withState compose from 'recompose' const enhancer = const MyComponent =
With functional component.
import React from 'react'import withSizes from 'react-sizes' const MyComponent = isMobile <div>isMobile ? 'Is Mobile' : 'Is Not Mobile'</div> const mapSizesToProps = width isMobile: width < 480 mapSizesToPropsMyComponent
Mess with props.
(Added in 0.1.0)
import React from 'react'import withSizes from 'react-sizes' const MyComponent = isMobile <div>isMobile ? 'Is Mobile' : 'Is Not Mobile'</div> const mapSizesToProps = width mobileBreakpoint isMobile: width < mobileBreakpoint mapSizesToPropsMyComponent
then:
<MyComponent = /><MyComponent = /><MyComponent = />
With presets selectors.
- const mapSizesToProps = ({ width }) => ({- isMobile: width < 480,- }); + const mapSizesToProps = sizes => ({+ isMobile: withSizes.isMobile(sizes),+ });
Presets Selectors
You can check all our presets selectors at our main code src/withSizes.js
.
withSizes width < 480withSizes width >= 480 && width < 1024withSizes width >= 1024 withSizes !withSizeswithSizes withSizes withSizes withSizeswithSizes !withSizes withSizes !withSizeswithSizes !withSizes
If it don't fit to your needs, you can create your own selectors.
// utils/sizes/selectors.jsconst isntDesktop = width width < 1024const backgroundColor = width width < 480 ? 'red' : 'green' // your componentimport isntDesktop backgroundColor from 'utils/sizes/selectors' const mapSizesToProps = canDisplayMobileFeature: backgroundColor:
sizes
argument is an object withwidth
andheight
properties and represents DOM window width and height.
Guide
mapSizesToProps(sizes)
sizes
argument is an object with width
and height
of DOM window.
const mapSizesToProps = { console // { width: 1200, height: 720 } (example)}
In pratice, it is a callback that return props that will injected into your Component.
const mapSizesToProps = { const props = backgroundColor: sizeswidth < 700 ? 'red' : 'green' return props}
But you can simplify this to stay practical and elegant.
const mapSizesToProps = backgroundColor: width < 700 ? 'red' : 'green'
Server Side Rendering
Since React Sizes rely on window to computate sizes, we can't computate the values in server enviroment. To try to get around this we can guess user viewport based on your user-agent, and pass values by a Context Provider.
But be careful, user-agent based detection is not a reliable solution. It's a workaround.
// Config can be created based on user-agent. See belowconst config = fallbackWidth: 360 fallbackHeight: 640 return <SizesProvider config=config> <App /> </SizesProvider>
Example:
// All other imports const getSizesFallback = { const md = userAgent if !!md return fallbackWidth: 360 fallbackHeight: 640 else if !!md return fallbackWidth: 768 fallbackHeight: 1024 return fallbackWidth: 1280 fallbackHeight: 700 } // Note: you don't need to use express, this is just an exampleconst app = app app
Performance Notes
Shallow Compare
React Sizes do a shallow compare in props generated from mapSizesToProps
(called propsToPass
), so it will only rerender when they really change. If you create a deep data sctructure, this can generate false positives. In these cases, we recommend using immutable for a more reliable shallow compare result. Or just don't use deep data structures, if possible.
Contribute
You can help improving this project sending PRs and helping with issues.
Also you ping me at Twitter