Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
react-sizes
Advanced tools
yarn add react-sizes
npm install react-sizes
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.
import React, { Component } from 'react';
import withSizes from 'react-sizes';
class MyComponent extends Component {
render() {
return (
<div>{this.props.isMobile ? 'Is Mobile' : 'Is Not Mobile'}</div>
);
}
}
const mapSizesToProps = ({ width }) => ({
isMobile: width < 480,
});
export default withSizes(mapSizesToProps)(MyComponent);
You can play with this example here.
import React from 'react';
import withSizes from 'react-sizes';
@withSizes(({ width }) => ({ isMobile: width < 480 }))
class MyComponent extends Component {
render() {
return (
<div>{this.props.isMobile ? 'Is Mobile' : 'Is Not Mobile'}</div>
);
}
}
export default MyComponent;
import React from 'react';
import withSizes from 'react-sizes';
import { withState, compose } from 'recompose';
const enhancer = compose(
withState('counter', 'setCounter', 0),
withSizes(({ width }) => ({ isMobile: width < 480 })),
);
const MyComponent = enhancer(({ isMobile, counter, setCounter }) => (
<div>
<div>
Count: {counter} <button onClick={() => setCounter(n => n + 1)}>Increment</button>
</div>
<div>{isMobile ? 'Is Mobile' : 'Is Not Mobile'}</div>
</div>
));
export default MyComponent;
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,
});
export default withSizes(mapSizesToProps)(MyComponent);
(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,
});
export default withSizes(mapSizesToProps)(MyComponent);
then:
<MyComponent mobileBreakpoint={480} />
<MyComponent mobileBreakpoint={400} />
<MyComponent mobileBreakpoint={600} />
- const mapSizesToProps = ({ width }) => ({
- isMobile: width < 480,
- });
+ const mapSizesToProps = sizes => ({
+ isMobile: withSizes.isMobile(sizes),
+ });
You can check all our presets selectors at our main code src/withSizes.js
.
withSizes.isMobile = ({ width }) => width < 480;
withSizes.isTablet = ({ width }) => width >= 480 && width < 1024;
withSizes.isDesktop = ({ width }) => width >= 1024;
withSizes.isGtMobile = (sizes) => !withSizes.isMobile(sizes);
withSizes.isGtTablet = (sizes) => withSizes.isDesktop(sizes);
withSizes.isStTablet = (sizes) => withSizes.isMobile(sizes);
withSizes.isStDesktop = (sizes) => !withSizes.isStDesktop(sizes);
withSizes.isTabletAndGreater = (sizes) => !withSizes.isMobile(sizes);
withSizes.isTabletAndSmaller = (sizes) => !withSizes.isStDesktop(sizes);
If it don't fit to your needs, you can create your own selectors.
// utils/sizes/selectors.js
export const isntDesktop = ({ width }) => width < 1024;
export const backgroundColor = ({ width }) => width < 480 ? 'red' : 'green';
// your component
import { isntDesktop, backgroundColor } from 'utils/sizes/selectors';
const mapSizesToProps = sizes => ({
canDisplayMobileFeature: isntDesktop(sizes),
backgroundColor: backgroundColor(sizes),
});
sizes
argument is an object withwidth
andheight
properties and represents DOM window width and height.
sizes
argument is an object with width
and height
of DOM window.
const mapSizesToProps = sizes => {
console.log(sizes) // { width: 1200, height: 720 } (example)
};
In pratice, it is a callback that return props that will injected into your Component.
const mapSizesToProps = function(sizes) {
const props = {
backgroundColor: sizes.width < 700 ? 'red' : 'green',
};
return props;
};
But you can simplify this to stay practical and elegant.
const mapSizesToProps = ({ width }) => ({
backgroundColor: width < 700 ? 'red' : 'green',
});
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.
You can help improving this project sending PRs and helping with issues.
Also you ping me at Twitter
FAQs
Hoc to easily map window sizes to props.
The npm package react-sizes receives a total of 2,394 weekly downloads. As such, react-sizes popularity was classified as popular.
We found that react-sizes demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.