react-component-error-boundary 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # react-component-boundary
2
+
3
+ A lightweight, reusable Error Boundary for React components that follows clean component design principles.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install react-component-error-boundary
9
+ ```
10
+
11
+ ## Basic Usage
12
+
13
+ ```jsx
14
+ import { ErrorBoundary } from "react-component-boundary";
15
+
16
+ <ErrorBoundary customErrorMessage="Something went wrong">
17
+ <YourComponent />
18
+ </ErrorBoundary>;
19
+ ```
20
+
21
+ ## Custom Error UI
22
+
23
+ ```jsx
24
+ <ErrorBoundary fallback={<div>Custom Error UI</div>}>
25
+ <YourComponent />
26
+ </ErrorBoundary>
27
+ ```
28
+
29
+ ## Props API
30
+
31
+ ```ts
32
+ interface ErrorBoundaryProps {
33
+ children: React.ReactNode;
34
+ customErrorMessage?: string;
35
+ fallback?: React.ReactNode;
36
+ }
37
+ ```
38
+
39
+ # Why this package?
40
+
41
+ React Error Boundaries often become:
42
+
43
+ - tightly coupled
44
+ - hard to reuse
45
+ - mixed with UI logic
46
+
47
+ This package keeps it simple and flexible:
48
+
49
+ - Parent owns state
50
+ - Child only renders
51
+ - Error UI is fully customizable
@@ -0,0 +1,24 @@
1
+ import * as React from 'react';
2
+
3
+ interface ErrorBoundaryProps {
4
+ children: React.ReactNode;
5
+ customErrorMessage?: string;
6
+ className?: string;
7
+ fallback: React.ReactNode;
8
+ onError?: (hasError: boolean) => void;
9
+ resetKey?: number;
10
+ }
11
+ interface ErrorBoundaryState {
12
+ hasError: boolean;
13
+ error: Error | null;
14
+ }
15
+ declare class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
16
+ constructor(props: ErrorBoundaryProps);
17
+ static getDerivedStateFromError(error: Error): ErrorBoundaryState;
18
+ componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void;
19
+ componentDidUpdate(prevProps: Readonly<ErrorBoundaryProps>, prevState: Readonly<ErrorBoundaryState>): void;
20
+ private logErrorToMyService;
21
+ render(): React.ReactNode;
22
+ }
23
+
24
+ export { ErrorBoundary, type ErrorBoundaryProps };
@@ -0,0 +1,24 @@
1
+ import * as React from 'react';
2
+
3
+ interface ErrorBoundaryProps {
4
+ children: React.ReactNode;
5
+ customErrorMessage?: string;
6
+ className?: string;
7
+ fallback: React.ReactNode;
8
+ onError?: (hasError: boolean) => void;
9
+ resetKey?: number;
10
+ }
11
+ interface ErrorBoundaryState {
12
+ hasError: boolean;
13
+ error: Error | null;
14
+ }
15
+ declare class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
16
+ constructor(props: ErrorBoundaryProps);
17
+ static getDerivedStateFromError(error: Error): ErrorBoundaryState;
18
+ componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void;
19
+ componentDidUpdate(prevProps: Readonly<ErrorBoundaryProps>, prevState: Readonly<ErrorBoundaryState>): void;
20
+ private logErrorToMyService;
21
+ render(): React.ReactNode;
22
+ }
23
+
24
+ export { ErrorBoundary, type ErrorBoundaryProps };
package/dist/index.js ADDED
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ ErrorBoundary: () => ErrorBoundary
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+
37
+ // src/ErrorBoundary.tsx
38
+ var React = __toESM(require("react"));
39
+ var import_jsx_runtime = require("react/jsx-runtime");
40
+ var ErrorBoundary = class extends React.Component {
41
+ constructor(props) {
42
+ super(props);
43
+ this.state = { hasError: false, error: null };
44
+ }
45
+ static getDerivedStateFromError(error) {
46
+ return { hasError: true, error };
47
+ }
48
+ componentDidCatch(error, errorInfo) {
49
+ var _a, _b, _c;
50
+ this.logErrorToMyService(
51
+ error,
52
+ (_a = errorInfo.componentStack) != null ? _a : void 0
53
+ // Note: React.captureOwnerStack() may not be available in all React versions
54
+ // Remove or conditionally use based on your React version
55
+ );
56
+ (_c = (_b = this.props).onError) == null ? void 0 : _c.call(_b, true);
57
+ }
58
+ componentDidUpdate(prevProps, prevState) {
59
+ var _a, _b;
60
+ if (this.props.resetKey !== void 0 && this.props.resetKey !== prevProps.resetKey && this.state.hasError) {
61
+ this.setState({ hasError: false, error: null });
62
+ }
63
+ if (prevState.hasError !== this.state.hasError) {
64
+ (_b = (_a = this.props).onError) == null ? void 0 : _b.call(_a, this.state.hasError);
65
+ }
66
+ }
67
+ logErrorToMyService(error, componentStack) {
68
+ console.error("Error caught by ErrorBoundary:");
69
+ console.error("Message:", error.message);
70
+ console.error("Stack:", error.stack);
71
+ console.error("Name:", error.name);
72
+ if (componentStack) {
73
+ console.error("Component stack:", componentStack);
74
+ }
75
+ console.error("Full error object:", error);
76
+ }
77
+ render() {
78
+ var _a;
79
+ if (this.state.hasError) {
80
+ return this.props.fallback || /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: this.props.className, children: (_a = this.props.customErrorMessage) != null ? _a : "Something went wrong." });
81
+ }
82
+ return this.props.children;
83
+ }
84
+ };
85
+ // Annotate the CommonJS export names for ESM import in node:
86
+ 0 && (module.exports = {
87
+ ErrorBoundary
88
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,51 @@
1
+ // src/ErrorBoundary.tsx
2
+ import * as React from "react";
3
+ import { jsx } from "react/jsx-runtime";
4
+ var ErrorBoundary = class extends React.Component {
5
+ constructor(props) {
6
+ super(props);
7
+ this.state = { hasError: false, error: null };
8
+ }
9
+ static getDerivedStateFromError(error) {
10
+ return { hasError: true, error };
11
+ }
12
+ componentDidCatch(error, errorInfo) {
13
+ var _a, _b, _c;
14
+ this.logErrorToMyService(
15
+ error,
16
+ (_a = errorInfo.componentStack) != null ? _a : void 0
17
+ // Note: React.captureOwnerStack() may not be available in all React versions
18
+ // Remove or conditionally use based on your React version
19
+ );
20
+ (_c = (_b = this.props).onError) == null ? void 0 : _c.call(_b, true);
21
+ }
22
+ componentDidUpdate(prevProps, prevState) {
23
+ var _a, _b;
24
+ if (this.props.resetKey !== void 0 && this.props.resetKey !== prevProps.resetKey && this.state.hasError) {
25
+ this.setState({ hasError: false, error: null });
26
+ }
27
+ if (prevState.hasError !== this.state.hasError) {
28
+ (_b = (_a = this.props).onError) == null ? void 0 : _b.call(_a, this.state.hasError);
29
+ }
30
+ }
31
+ logErrorToMyService(error, componentStack) {
32
+ console.error("Error caught by ErrorBoundary:");
33
+ console.error("Message:", error.message);
34
+ console.error("Stack:", error.stack);
35
+ console.error("Name:", error.name);
36
+ if (componentStack) {
37
+ console.error("Component stack:", componentStack);
38
+ }
39
+ console.error("Full error object:", error);
40
+ }
41
+ render() {
42
+ var _a;
43
+ if (this.state.hasError) {
44
+ return this.props.fallback || /* @__PURE__ */ jsx("div", { className: this.props.className, children: (_a = this.props.customErrorMessage) != null ? _a : "Something went wrong." });
45
+ }
46
+ return this.props.children;
47
+ }
48
+ };
49
+ export {
50
+ ErrorBoundary
51
+ };
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "react-component-error-boundary",
3
+ "version": "1.0.3",
4
+ "access": "public",
5
+ "description": "A reusable React Error Boundary",
6
+ "keywords": [
7
+ "react",
8
+ "error-boundary",
9
+ "next"
10
+ ],
11
+ "license": "ISC",
12
+ "author": "Ajay Yadav",
13
+ "main": "dist/index.cjs",
14
+ "module": "dist/index.js",
15
+ "types": "dist/index.d.ts",
16
+ "files": [
17
+ "dist",
18
+ "README.md"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsup src/index.ts --format esm,cjs --dts",
22
+ "prepublishOnly": "npm run build"
23
+ },
24
+ "peerDependencies": {
25
+ "react": ">=16.8",
26
+ "react-dom": ">=16.8"
27
+ },
28
+ "dependencies": {
29
+ "react": ">=16.8",
30
+ "react-dom": ">=16.8"
31
+ },
32
+ "devDependencies": {
33
+ "@types/react": "^19.2.7",
34
+ "@types/react-dom": "^19.2.3",
35
+ "tsup": "^8.5.1",
36
+ "typescript": "^5.9.3"
37
+ }
38
+ }