Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrap preview in boundary #392

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix: throw away Boundary on new code
  • Loading branch information
r281GQ committed Aug 19, 2024
commit a0d276e6b3eb29a0ff3ceac90c0f6d4b40c91300
1 change: 1 addition & 0 deletions packages/react-live/src/components/Live/LiveContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type ContextValue = {
error?: string;
element?: ComponentType | null;
code: string;
newCode?: string;
disabled: boolean;
language: string;
theme?: typeof themes.nightOwl;
Expand Down
8 changes: 6 additions & 2 deletions packages/react-live/src/components/Live/LivePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class ErrorBoundary extends Component<
}

render() {
if (this.state.hasError) {
return null;
}

return this.props.children;
}
}
Expand All @@ -39,11 +43,11 @@ function LivePreview<T extends keyof JSX.IntrinsicElements>(
function LivePreview<T extends React.ElementType>(props: Props<T>): JSX.Element;

function LivePreview({ Component = "div", ...rest }: Props): JSX.Element {
const { element: Element, onError, code } = useContext(LiveContext);
const { element: Element, onError, newCode } = useContext(LiveContext);

return (
<ErrorBoundary
key={code}
key={newCode}
onError={(err) => {
onError(err);
}}
Expand Down
15 changes: 12 additions & 3 deletions packages/react-live/src/components/Live/LiveProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { themes } from "prism-react-renderer";
type ProviderState = {
element?: ComponentType | null;
error?: string;
newCode?: string;
};

type Props = {
Expand Down Expand Up @@ -37,7 +38,11 @@ function LiveProvider({

async function transpileAsync(newCode: string) {
const errorCallback = (error: Error) => {
setState({ error: error.toString(), element: undefined });
setState((previousState) => ({
...previousState,
error: error.toString(),
element: undefined,
}));
};

// - transformCode may be synchronous or asynchronous.
Expand All @@ -51,7 +56,7 @@ function LiveProvider({
try {
const transformedCode = await Promise.resolve(transformResult);
const renderElement = (element: ComponentType) =>
setState({ error: undefined, element });
setState({ error: undefined, element, newCode });

if (typeof transformedCode !== "string") {
throw new Error("Code failed to transform");
Expand All @@ -65,7 +70,11 @@ function LiveProvider({
};

if (noInline) {
setState({ error: undefined, element: null }); // Reset output for async (no inline) evaluation
setState((previousState) => ({
...previousState,
error: undefined,
element: null,
})); // Reset output for async (no inline) evaluation
renderElementAsync(input, renderElement, errorCallback);
} else {
renderElement(generateElement(input, errorCallback));
Expand Down