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

React 16 componentDidCatch

HI I can not get the oh no something went wrong to be displayed as the fallback UI .

I thought I had made a mistake but I downloaded the final version and it does not display it.

I tried changing hasError to true, and the fallback is displayed here is the code:

import React, { Component } from 'react';

export default class User extends Component {

  state = {
     hasError: false,
     student: {
       name: '',
     },
    };


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



  render() {

    const { name } = this.state.student;
    if (this.state.hasError) {
      return <h1> Oh no something went wrong </h1>
     }  else  {
       return (
         <div>
           <input
             type="text"
             placeholder="Enter name..."
             onChange={e => this.updateStudentName(e.target.value)}
             value={name}
           />

           <button
             type="button"
             onClick={() => this.setState({ student: null })}
           >Reset Student</button>

           <p><strong>Student Name:</strong> {name}</p>
         </div>
       );

     }

  }
}

I guess it just does not work on some browsers?