react-component-boundary 1.0.0

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.
@@ -0,0 +1,24 @@
1
+ import * as React from 'react';
2
+
3
+ interface ComponentBoundaryProps {
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 ComponentBoundaryState {
12
+ hasError: boolean;
13
+ error: Error | null;
14
+ }
15
+ declare class ComponentBoundary extends React.Component<ComponentBoundaryProps, ComponentBoundaryState> {
16
+ constructor(props: ComponentBoundaryProps);
17
+ static getDerivedStateFromError(error: Error): ComponentBoundaryState;
18
+ componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void;
19
+ componentDidUpdate(prevProps: Readonly<ComponentBoundaryProps>, prevState: Readonly<ComponentBoundaryState>): void;
20
+ private logErrorToMyService;
21
+ render(): React.ReactNode;
22
+ }
23
+
24
+ export { ComponentBoundary, type ComponentBoundaryProps };
@@ -0,0 +1,24 @@
1
+ import * as React from 'react';
2
+
3
+ interface ComponentBoundaryProps {
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 ComponentBoundaryState {
12
+ hasError: boolean;
13
+ error: Error | null;
14
+ }
15
+ declare class ComponentBoundary extends React.Component<ComponentBoundaryProps, ComponentBoundaryState> {
16
+ constructor(props: ComponentBoundaryProps);
17
+ static getDerivedStateFromError(error: Error): ComponentBoundaryState;
18
+ componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void;
19
+ componentDidUpdate(prevProps: Readonly<ComponentBoundaryProps>, prevState: Readonly<ComponentBoundaryState>): void;
20
+ private logErrorToMyService;
21
+ render(): React.ReactNode;
22
+ }
23
+
24
+ export { ComponentBoundary, type ComponentBoundaryProps };
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
+ ComponentBoundary: () => ComponentBoundary
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+
37
+ // src/ComponentBoundary.tsx
38
+ var React = __toESM(require("react"));
39
+ var import_jsx_runtime = require("react/jsx-runtime");
40
+ var ComponentBoundary = 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 ComponentBoundary:");
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
+ ComponentBoundary
88
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,51 @@
1
+ // src/ComponentBoundary.tsx
2
+ import * as React from "react";
3
+ import { jsx } from "react/jsx-runtime";
4
+ var ComponentBoundary = 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 ComponentBoundary:");
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
+ ComponentBoundary
51
+ };
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "react-component-boundary",
3
+ "version": "1.0.0",
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
+ ],
19
+ "scripts": {
20
+ "build": "tsup src/index.ts --format esm,cjs --dts",
21
+ "prepublishOnly": "npm run build"
22
+ },
23
+ "peerDependencies": {
24
+ "react": ">=16.8",
25
+ "react-dom": ">=16.8"
26
+ },
27
+ "dependencies": {
28
+ "react": ">=16.8",
29
+ "react-dom": ">=16.8"
30
+ },
31
+ "devDependencies": {
32
+ "@types/react": "^19.2.7",
33
+ "@types/react-dom": "^19.2.3",
34
+ "tsup": "^8.5.1",
35
+ "typescript": "^5.9.3"
36
+ }
37
+ }