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

Add onError prop to LiveProvider #163

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ It supports these props, while passing any others through to the `children`:
|scope|PropTypes.object|Accepts custom globals that the `code` can use
|noInline|PropTypes.bool|Doesn’t evaluate and mount the inline code (Default: `false`). Note: when using `noInline` whatever code you write must be a single expression (function, class component or some `jsx`) that can be returned immediately. If you'd like to render multiple components, use `noInline={true}`
|transformCode|PropTypes.func|Accepts and returns the code to be transpiled, affording an opportunity to first transform it
|onRender|PropTypes.func|Called when component is rendered (un)successfully. In the case of an unsuccessful render, an `Error` object is passed as the only argument.|
|language|PropTypes.string|What language you're writing for correct syntax highlighting. (Default: `jsx`)
|disabled|PropTypes.bool|Disable editing on the `<LiveEditor />` (Default: `false`)
|theme|PropTypes.object|A `prism-react-renderer` theme object. See more [here](https://github.com/FormidableLabs/prism-react-renderer#theming)
Expand Down Expand Up @@ -243,5 +244,5 @@ Here are the various factors at play:

## Maintenance Status

**Active:** Formidable is actively working on this project, and we expect to continue for work for the foreseeable future. Bug reports, feature requests and pull requests are welcome.
**Active:** Formidable is actively working on this project, and we expect to continue for work for the foreseeable future. Bug reports, feature requests and pull requests are welcome.

20 changes: 18 additions & 2 deletions src/components/Live/LiveProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default class LiveProvider extends Component {
disabled: PropTypes.bool,
language: PropTypes.string,
noInline: PropTypes.bool,
onRender: PropTypes.func,
scope: PropTypes.object,
theme: PropTypes.object,
transformCode: PropTypes.node
Expand Down Expand Up @@ -54,6 +55,9 @@ export default class LiveProvider extends Component {

onError = error => {
this.setState({ error: error.toString() });
if (this.props.onRender) {
this.props.onRender(error);
}
};

transpile = ({ code, scope, transformCode, noInline = false }) => {
Expand All @@ -63,9 +67,18 @@ export default class LiveProvider extends Component {
scope
};

const errorCallback = err =>
const errorCallback = err => {
this.setState({ element: undefined, error: err.toString() });
const renderElement = element => this.setState({ ...state, element });
if (this.props.onRender) {
this.props.onRender(err);
}
};
const renderElement = element => {
this.setState({ ...state, element });
if (this.props.onRender) {
this.props.onRender();
}
};

// State reset object
const state = { unsafeWrapperError: undefined, error: undefined };
Expand All @@ -79,6 +92,9 @@ export default class LiveProvider extends Component {
}
} catch (error) {
this.setState({ ...state, error: error.toString() });
if (this.props.onRender) {
this.props.onRender(error);
}
}
};

Expand Down
1 change: 1 addition & 0 deletions typings/react-live.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type LiveProviderProps = Omit<DivProps, 'scope'> & {
language?: Language;
disabled?: boolean;
theme?: PrismTheme;
onRender?: (error?: Error) => void;
}

export const LiveProvider: ComponentClass<LiveProviderProps>
Expand Down