rc-util: Common React Utilities

5.44.4 · active · verified Sun Apr 19

rc-util is a foundational utility library within the React ecosystem, specifically developed to provide common helper functions and components for other React libraries (often those under the `react-component` organization like Ant Design). It offers a suite of low-level utilities such as `createChainedFunction` for combining multiple event handlers, a `deprecated` logger for warning about API changes, and `Portal` for rendering children outside the DOM hierarchy. The current stable version is `5.44.4`. While a specific, detailed release cadence for `rc-util` is not directly specified in the available metadata, the project is actively maintained, receiving updates to address various React development challenges and support newer React versions, as evidenced by related package activity. Its key differentiator lies in its focused, unopinionated approach, providing essential building blocks without imposing architectural patterns, making it a reliable dependency for complex UI component libraries.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `createChainedFunction` to combine multiple event handlers and `Portal` to render content into a specific DOM node, which is useful for overlays or modals.

import React, { useRef, useState } from 'react';
import { createRoot } from 'react-dom/client';
import createChainedFunction from 'rc-util/lib/createChainedFunction';
import Portal from 'rc-util/lib/Portal';

const App = () => {
  const [log, setLog] = useState<string[]>([]);
  const containerRef = useRef<HTMLDivElement>(null);

  const handleClick1 = () => {
    setLog(prev => [...prev, 'Function 1 called!']);
  };

  const handleClick2 = () => {
    setLog(prev => [...prev, 'Function 2 called!']);
  };

  const chainedClickHandler = createChainedFunction(handleClick1, handleClick2);

  const getPortalContainer = () => {
    if (!containerRef.current) {
      // Create container if it doesn't exist
      const div = document.createElement('div');
      div.id = 'portal-root';
      document.body.appendChild(div);
      containerRef.current = div;
    }
    return containerRef.current;
  };

  return (
    <div>
      <h1>rc-util Demo</h1>
      <button onClick={chainedClickHandler}>
        Click Me (Chained Functions)
      </button>
      <div style={{ marginTop: '20px', border: '1px solid #ccc', padding: '10px' }}>
        <h2>Event Log:</h2>
        {log.length === 0 ? <p>No events yet.</p> : log.map((msg, i) => <p key={i}>{msg}</p>)}
      </div>

      <div ref={containerRef} style={{ border: '1px dashed blue', padding: '10px', marginTop: '20px' }}>
        Portal Mount Point (conceptually inside App, visually rendered here)
      </div>

      {/* Content rendered into the container obtained from getPortalContainer */}
      <Portal getContainer={getPortalContainer}>
        <div style={{ background: 'lightgreen', padding: '10px', border: '1px solid green' }}>
          This content is rendered via Portal!
        </div>
      </Portal>
    </div>
  );
};

const rootElement = document.getElementById('root');
if (rootElement) {
  createRoot(rootElement).render(<App />);
} else {
  console.error("Root element with ID 'root' not found in the document.");
}

view raw JSON →