Home > Software design >  React and react native: Unexpected token. A constructor, method, accessor, or property was expected.
React and react native: Unexpected token. A constructor, method, accessor, or property was expected.

Time:01-14

I'm studying react-native. I have a problem I cannot solve it. State in react class component gives me error: unexpected token. But useState in functional component works fine. Sorry for my language. I'm from Mongolia. Here is my class and functional component: react-native-class-component

react-native-functional-component

How can I solve it?

CodePudding user response:

Here in your class component you need to declare your state inside constructor like this

 export default class Home extends Component{
         constructor(props){
            this.state = {}
         }
         render(){
             return(
                 <View>
                       <Text></Text>
                 </View>
              )
         }
    }

CodePudding user response:

The way you have declared the state in your class component is wrong. It should be within your constructor.

export default class Home extends Component{
       constructor(props){
            this.state = {}
         }
         render(){
             return(
                 …
              )
         }
    }
  •  Tags:  
  • Related