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

How to handle/catch errors when rendering components in router #2338

Closed
syltaxue opened this issue Oct 21, 2015 · 4 comments
Closed

How to handle/catch errors when rendering components in router #2338

syltaxue opened this issue Oct 21, 2015 · 4 comments

Comments

@syltaxue
Copy link

Hi Guys,

When we have errors in a component of the router, the entire tree might get taken down.
For example, in the router below, we have many route paths, and each path is a react component. When rendering any component, if there is an error, the router then gets to a bad state, and any further actions(e.g. a user click a link to render another route component ) may fail.
What would be the work around to catch/handle the errors in component rendering? And is there a way to handle the errors in the components level?

The use case here is:
In a component, I am rendering a basic framework + some other children components. Then I get errors when rendering the children components.
In this scenario, I want to keep the basic framework + render some errors messages to user, so I might need to find a way to handle/catch the errors in the component. Any ideas or suggestions?

render((
  <Router>
    <Route path="/" component={App}>
      <Route path="about" component={About}/>
      <Route path="users" component={Users}>
        <Route path="/user/:userId" component={User}/>
      </Route>
    </Route>
  </Router>
), document.body)
@taion
Copy link
Contributor

taion commented Oct 21, 2015

This is not really React Router specific. You'd catch the component error however you do normally with buggy render functions, and then handle that error somehow.

But otherwise this is no different from if you were rendering the component tree without React Router.

@taion taion closed this as completed Oct 21, 2015
@agundermann
Copy link
Contributor

This seems related: facebook/react#2461

@syltaxue
Copy link
Author

How to catch the errors if my render function simply just returns some other children components, and the errors are thrown from the children components? And sometimes the components we use may come from some third party libraries where not all the errors are handled.

    render () {
        return (
            <div className="Framework">
                <some layouts in general framework>
                <Component1>
                <Component2>
                <Component3>
            </div>
        );
    }

And if any error occurs in the children render function,
I want to catch/handle the errors in the parent component level, because I want to show something like

    render () {
        return (
            <div className="Framework">
                <some layouts in general framework>
                <some errors>
            </div>
        );
    }

@taion

@taion
Copy link
Contributor

taion commented Oct 21, 2015

The same way you'd do it without React Router. There's just nothing RR specific here.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants