Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Catching Errors with Error Boundaries

Victor Cuc
Victor Cuc
7,281 Points

What does return (this.props.children); do?

What does return (this.props.children); do in:

import React, { Component } from 'react';

export default class ErrorBoundary extends Component {
  state = {
    hasError: false
  }

  componentDidCatch() {
    this.setState({ hasError: true });
  }

  render() {
    if (this.state.hasError) {
      return <h2>Oh no! Something went wrong.</h2>
    }
    return (this.props.children);
  }
}

1 Answer

Jordan Wren
Jordan Wren
30,940 Points

What it does is return its children. In the case of this example, we wrapped the <ErrorBoundary> component around the <StudentForm> component, making the <StudentForm> component a child of <ErrorBoundary>. Therefore, when an error does not occur, the <StudentForm> component will be rendered.