localedate
v1.1.0
Published
A helper module for ensuring typesafety of react components on node.js
Readme
reactnode
reactnode is a lightweight utility library focused on providing type-safe validation, inspection, and safe element creation for React components used in Node.js environments. It is suited for SSR tools, command-line utilities, testing frameworks, and any workflow that requires React without a browser DOM.
Features
- Type-safe validation of React component props
- Node-safe React element creation helpers
- Component-type detection (functional vs class)
- Prop-shape introspection utilities
- Full TypeScript support with bundled type declarations
- Zero external configuration required
Installation
npm install reactnode
yarn add reactnode
pnpm add reactnode
Usage
Example: validating a component's props
import { validateComponentProps } from "reactnode"; import React from "react";
function Button(props: { label: string; disabled?: boolean }) { return React.createElement("button", { disabled: props.disabled }, props.label); }
const result = validateComponentProps(Button, { label: "Click me" });
console.log(result.success); console.log(result.errors);
Example: detecting component types
import { isFunctionalComponent, isClassComponent } from "reactnode"; import React from "react";
function A() { return null; }
class B extends React.Component { render() { return null; } }
console.log(isFunctionalComponent(A)); console.log(isClassComponent(B));
Example: safe creation of React elements
import { safeCreateElement } from "reactnode"; import React from "react";
const element = safeCreateElement("div", { id: "root" }, "Hello");
console.log(element.type); console.log(element.props.id);
API Overview
validateComponentProps(Component, props)
Validates props passed into a React component based on inferred prop types.
Returns an object with:
success: boolean
errors: string[]
isFunctionalComponent(Component)
Returns true if the provided component is a React function component.
isClassComponent(Component)
Returns true if the component is a React class component.
safeCreateElement(type, props, ...children)
A wrapper around React.createElement designed to behave safely in non-DOM environments, such as Node.js.
inspectProps(Component)
Extracts metadata describing the component's expected props, including names and optionality.
Build and Development
reactnode uses tsup for bundling. The build output includes:
lib/index.js (CommonJS)
lib/index.mjs (ES Module)
lib/index.d.ts (TypeScript type definitions)
Build the library:
npm run build
Start development mode:
npm run dev
Testing
reactnode uses Vitest for testing.
Run tests with:
npm test
Package Contents
The published package includes only:
lib/
README.md
package.json
LICENSE
TypeScript source files are excluded from the published bundle.
License
MIT License
