Unexpected error with state={} in react class component
See original GitHub issueHey 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:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
https://babeljs.io/docs/plugins/transform-class-properties/
I think you should use
super()
inconstructor() {}
before you usethis
when u extend a class. In react docs, it’s