Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
The rc-util npm package is a collection of utility functions and components designed to be used with React components. It provides a set of tools to help with common tasks in React development such as DOM manipulation, event handling, and component management.
DOM manipulation
This feature allows you to get the scroll position of an element or the window. It is useful for handling scroll-related logic in your React components.
import { getScroll } from 'rc-util/lib/Dom/scroll';
const scrollPosition = getScroll(window);
console.log(scrollPosition);
Event handling
This feature provides a way to add and remove event listeners. It is particularly useful for setting up event listeners with the capability to easily remove them later, preventing potential memory leaks.
import { addEventListener } from 'rc-util/lib/Dom/addEventListener';
const removeListener = addEventListener(window, 'resize', () => {
console.log('Window resized');
});
// To remove the event listener
removeListener();
KeyCode utilities
KeyCode utilities provide a convenient set of constants for key codes, which can be used to improve the readability and maintainability of code that involves keyboard event handling.
import KeyCode from 'rc-util/lib/KeyCode';
const isEnterPressed = (event) => {
return event.keyCode === KeyCode.ENTER;
};
Lodash is a comprehensive utility library that offers a wide range of functions for tasks like array manipulation, object handling, and function utilities. It is more general-purpose compared to rc-util, which is more focused on React-specific utilities.
Underscore is another utility library similar to lodash, providing a range of functions for manipulating data structures and performing common JavaScript tasks. Like lodash, it is not specifically tailored to React.
Common Utils For React Component.
(...functions): Function
Create a function which will call all the functions with it's arguments from left to right.
import createChainedFunction from 'rc-util/lib/createChainedFunction';
(prop: string, instead: string, component: string): void
Log an error message to warn developers that prop
is deprecated.
import deprecated from 'rc-util/lib/deprecated';
(config: Object): Object
To generate a mixin which will render specific component into specific container automatically.
import getContainerRenderMixin from 'rc-util/lib/getContainerRenderMixin';
Fields in config
and their meanings.
Field | Type | Description | Default |
---|---|---|---|
autoMount | boolean | Whether to render component into container automatically | true |
autoDestroy | boolean | Whether to remove container automatically while the component is unmounted | true |
isVisible | (instance): boolean | A function to get current visibility of the component | - |
getComponent | (instance, extra): ReactNode | A function to get the component which will be rendered into container | - |
getContaienr | (instance): HTMLElement | A function to get the container |
(fresh?: boolean): number
Get the width of scrollbar.
import getScrollBarSize from 'rc-util/lib/getScrollBarSize';
(): string
To generate a global unique id across current application.
import guid from 'rc-util/lib/guid';
(props: Object): Object
Pick valid HTML attributes and events from props.
import pickAttrs from 'rc-util/lib/pickAttrs';
(msg: string): void
A shallow wrapper of console.warn
.
import warn from 'rc-util/lib/warn';
A collection of functions to operate React elements' children.
(children): children
Return a shallow copy of children.
import mapSelf from 'rc-util/lib/Children/mapSelf';
(children: ReactNode[]): ReactNode[]
Convert children into an array.
import toArray from 'rc-util/lib/Children/toArray';
A collection of functions to operate DOM elements.
(target: ReactNode, eventType: string, listener: Function): { remove: Function }
A shallow wrapper of add-dom-event-listener.
import addEventlistener from 'rc-util/lib/Dom/addEventlistener';
(): boolean
Check if DOM is available.
import casUseDom from 'rc-util/lib/Dom/casUseDom';
A collection of functions to operate DOM nodes' class name.
hasClass(node: HTMLElement, className: string): boolean
addClass(node: HTMLElement, className: string): void
removeClass(node: HTMLElement, className: string): void
import cssClass from 'rc-util/lib/Dom/class;
(root: HTMLElement, node: HTMLElement): boolean
Check if node is equal to root or in the subtree of root.
import contains from 'rc-util/lib/Dom/contains';
A collection of functions to get or set css styles.
get(node: HTMLElement, name?: string): any
set(node: HTMLElement, name?: string, value: any) | set(node, object)
getOuterWidth(el: HTMLElement): number
getOuterHeight(el: HTMLElement): number
getDocSize(): { width: number, height: number }
getClientSize(): { width: number, height: number }
getScroll(): { scrollLeft: number, scrollTop: number }
getOffset(node: HTMLElement): { left: number, top: number }
import css from 'rc-util/lib/Dom/css';
A collection of functions to operate focus status of DOM node.
saveLastFocusNode(): void
clearLastFocusNode(): void
backLastFocusNode(): void
getFocusNodeList(node: HTMLElement): HTMLElement[]
get a list of focusable nodes from the subtree of node.limitTabRange(node: HTMLElement, e: Event): void
import focus from 'rc-util/lib/Dom/focus';
{ animation: boolean | Object, transition: boolean | Object }
A flag to tell whether current environment supports animationend
or transitionend
.
import support from 'rc-util/lib/Dom/support';
Enum
Enum of KeyCode, please check the definition of it.
import KeyCode from 'rc-util/lib/KeyCode';
(e: Event): boolean
Whether text and modified key is entered at the same time.
(keyCode: KeyCode): boolean
Whether character is entered.
rc-util is released under the MIT license.
FAQs
Common Utils For React Component
We found that rc-util demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 9 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 News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.