@tanstack/react-router
Version:
Modern and scalable routing for React applications
115 lines (114 loc) • 3.54 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
function _interopNamespaceDefault(e) {
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
if (e) {
for (const k in e) {
if (k !== "default") {
const d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: () => e[k]
});
}
}
}
n.default = e;
return Object.freeze(n);
}
const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
function CatchBoundary(props) {
const errorComponent = props.errorComponent ?? ErrorComponent;
return /* @__PURE__ */ jsxRuntime.jsx(
CatchBoundaryImpl,
{
getResetKey: props.getResetKey,
onCatch: props.onCatch,
children: ({ error, reset }) => {
if (error) {
return React__namespace.createElement(errorComponent, {
error,
reset
});
}
return props.children;
}
}
);
}
class CatchBoundaryImpl extends React__namespace.Component {
constructor() {
super(...arguments);
this.state = { error: null };
}
static getDerivedStateFromProps(props) {
return { resetKey: props.getResetKey() };
}
static getDerivedStateFromError(error) {
return { error };
}
reset() {
this.setState({ error: null });
}
componentDidUpdate(prevProps, prevState) {
if (prevState.error && prevState.resetKey !== this.state.resetKey) {
this.reset();
}
}
componentDidCatch(error, errorInfo) {
if (this.props.onCatch) {
this.props.onCatch(error, errorInfo);
}
}
render() {
return this.props.children({
error: this.state.resetKey !== this.props.getResetKey() ? null : this.state.error,
reset: () => {
this.reset();
}
});
}
}
function ErrorComponent({ error }) {
const [show, setShow] = React__namespace.useState(process.env.NODE_ENV !== "production");
return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { padding: ".5rem", maxWidth: "100%" }, children: [
/* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", alignItems: "center", gap: ".5rem" }, children: [
/* @__PURE__ */ jsxRuntime.jsx("strong", { style: { fontSize: "1rem" }, children: "Something went wrong!" }),
/* @__PURE__ */ jsxRuntime.jsx(
"button",
{
style: {
appearance: "none",
fontSize: ".6em",
border: "1px solid currentColor",
padding: ".1rem .2rem",
fontWeight: "bold",
borderRadius: ".25rem"
},
onClick: () => setShow((d) => !d),
children: show ? "Hide Error" : "Show Error"
}
)
] }),
/* @__PURE__ */ jsxRuntime.jsx("div", { style: { height: ".25rem" } }),
show ? /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(
"pre",
{
style: {
fontSize: ".7em",
border: "1px solid red",
borderRadius: ".25rem",
padding: ".3rem",
color: "red",
overflow: "auto"
},
children: error.message ? /* @__PURE__ */ jsxRuntime.jsx("code", { children: error.message }) : null
}
) }) : null
] });
}
exports.CatchBoundary = CatchBoundary;
exports.ErrorComponent = ErrorComponent;
//# sourceMappingURL=CatchBoundary.cjs.map
;