question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected error with state={} in react class component

See original GitHub issue

Hey there,

I’m getting the following error:

/Users/tnrich/Sites/treeview/Comp.js:10:7: Unexpected token (10:7)                     elDigestEnzymes });
> 10 |   state={
     |        ^

from the following piece of code

export default class Ladder extends React.Component {
  state={
    highlightedFragment: undefined 
  }

The following code works and does the same thing, but I’d like to be able to do the top version.

export default class Ladder extends React.Component {
  // constructor(props) {
  //   super(props);
  //   this.state = { highlightedFragment: undefined };
  // }

Does anyone know which babel transform I need to enable to get this to work? Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
DimaLiLongJicommented, Jan 14, 2018

I think you should use super() in constructor() {} before you use this when u extend a class. In react docs, it’s

 constructor(props) {
    super(props);
    this.state = {date: new Date()};
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

React class component state unexpected error - Laracasts
React class component state unexpected error. Hello,. I have preset react and I defined a class without the constructor setting the state or...
Read more >
Unknown error in a state in react when trying to change it
I'm currently learning react and I receive an unknown error when trying to create a function that changes the state of a class...
Read more >
Error Handling in React 16 – React Blog
A class component becomes an error boundary if it defines a new lifecycle ... this.state = { hasError: false }; } componentDidCatch(error, ...
Read more >
React Error Boundaries: Complete Guide - Meticulous
class ErrorBoundarySimple extends React.Component { state = { hasError: false }; componentDidCatch(error: unknown) { // report the error to ...
Read more >
How to bind 'this' keyword to resolve classical error message ...
class App extends Component {. static defaultProps = {. courseContent : [. 'JSX' , 'React Props' , 'React State' ,.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found