
Security Fundamentals
Obfuscation 101: Unmasking the Tricks Behind Malicious Code
Attackers use obfuscation to hide malware in open source packages. Learn how to spot these techniques across npm, PyPI, Maven, and more.
react-measurable
Advanced tools
Decorate your React component and get notified when its dimensions change.
npm install react-measurable
React-measurable is compatible with React 16.3 and React strict mode since it uses the new createRef
API.
It also depends on resize-observer-polyfill for browsers that don't support the ResizeObserver API (non-Chrome). The performance depends entirely on the polyfill performance.
https://hfour.github.io/react-measurable/
@measurable
.componentDidMount
lifecycle method, make sure you call observeResize
.MeasurableProps
.import * as React from "react";
import { measurable, MeasurableProps } from "../src";
interface MyProps extends MeasurableProps {}
@measurable
export class MyComponent extends React.Component<MyProps> {
ref = React.createRef<HTMLDivElement>();
componentDidMount() {
this.props.observeResize(this.ref, (rect, target) => {
// Do something
});
}
render() {
return <div ref={this.ref}>A div that could change size</div>;
}
}
The decorator adds an additional prop to your component. The prop is a registration function which needs to be called in your componentDidMount
lifecycle method.
You need to pass a ref to your DOM node and a callback to the observeResize
function. The DOM node ref can be any DOM node, not necessarily the one rendered by the decorated component. The ref needs to be a React.RefObject
, supported in React 16.3. The callback will be called when a size change is observer and will be passed a DOMRectReadyOnly
object and a the actual observed DOM node.
// The Element and DOMRectReadOnly are standard DOM types.
export interface MeasurableProps extends React.Attributes {
observeResize?: <T extends Element>(
ref: React.RefObject<T>,
callback: (rect: DOMRectReadOnly, target: T) => void
) => void;
}
The decorator makes very little assumptions about how you use it. It does not perform extra measurements on components apart from what the polyfill does. It only notifies your component when its size changes, with a minimal API.
If you want notifications to parent components, you could do the following:
interface MyProps extends MeasurableProps {
// Passed by the parent to observe clild resizes
onResize: (rect: ClientRect) => void;
}
@measurable
export class MyComponent extends React.Component<MyProps> {
// ...
componentDidMount() {
this.props.observeResize(this.ref, (rect, target) => {
// Perform more measurements
this.props.onResize(getBoundingClientRect(target));
});
}
// ...
}
The decorator doesn't use prop-types because, well, typescript exists.
MIT
FAQs
Get notified when your React component changes dimensions
The npm package react-measurable receives a total of 0 weekly downloads. As such, react-measurable popularity was classified as not popular.
We found that react-measurable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers 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.
Security Fundamentals
Attackers use obfuscation to hide malware in open source packages. Learn how to spot these techniques across npm, PyPI, Maven, and more.
Security News
Join Socket for exclusive networking events, rooftop gatherings, and one-on-one meetings during BSidesSF and RSA 2025 in San Francisco.
Security News
Biome's v2.0 beta introduces custom plugins, domain-specific linting, and type-aware rules while laying groundwork for HTML support and embedded language features in 2025.