React RND is a versatile React component that combines both draggable and resizable functionalities into a single, cohesive unit. It allows developers to easily create UI elements that users can move around and change the dimensions of within their applications. The current stable version is 10.5.3, with a fairly active release cadence, frequently incorporating bug fixes and minor feature enhancements. Key differentiators include its robust API for controlling size and position (both controlled and uncontrolled modes), its ability to set drag and resize bounds, and its integration of underlying libraries like `react-draggable` and `re-resizable` to provide a comprehensive solution without requiring developers to combine these functionalities themselves. It ships with TypeScript types, facilitating safer development in larger codebases.
npm install react-rndVerified import paths — ran on the pinned version, not inferred.
This example demonstrates a controlled `Rnd` component that manages its size and position through React state using `useState` hooks. It updates the component's dimensions and coordinates on drag and resize stop events, and also sets minimum size constraints and bounds to the window.
Ensure `onDragStop` and `onResizeStop` callbacks update the state variables (`x`, `y`, `width`, `height`) that are passed to the `position` and `size` props respectively.
Choose either the `default` prop for an uncontrolled component or `size`/`position` props with state management for a controlled component. Do not use both simultaneously for the same properties.
Upgrade to `react-rnd@10.4.1` or newer to reliably pass an `HTMLElement` reference to the `bounds` prop. For older versions, use string selectors (e.g., `'body'`) or define bounds with `{ left, top, right, bottom }` coordinates.Stick to the `react-draggable` version specified in `react-rnd`'s `package-lock.json` or `yarn.lock`. If experiencing issues, verify the `react-draggable` version used by `react-rnd` via `npm ls react-draggable` and downgrade if necessary.
Ensure that if `position` and `size` props are used, they are always provided with valid numerical or string values, and are updated via `onDragStop` or `onResizeStop` handlers.
Use the correct ES module named import: `import { Rnd } from 'react-rnd';`. If strictly using CommonJS (less common in modern React), ensure it's `const Rnd = require('react-rnd').Rnd;`.Provide `bounds` as either a CSS selector string (e.g., `'parent'`), a direct `HTMLElement` reference (>=v10.4.1), or an object like `{ left: 0, top: 0, right: 100, bottom: 100 }`.