Error: TransformError SyntaxError: Unexpected token, expected ";" (8:20)

Error: TransformError SyntaxError: Unexpected token, expected ";" (8:20)

尝试在 React Native 中进行试验,但在我的 android 模拟器上一直出现错误,这是错误

错误:TransformError SyntaxError:意外的标记,应为“;” (8:20)

import React, { Component } from 'react';
import {View, Text, StyleSheet, Button} from 'react-native';

import Header from './components/Header';
import Input from './components/Input';

const App = () =>{
  constructor(props){
    super(props);

    this.state = {
      todoInput: '';
      todos:[
        {id: 0, title: 'sample1', done: false},
      ]
    }
  }

  function addNewLog(){
    console.log(this.state.todos);
  }

  render() {
    return (
      <View style={styles.container}>
        <Header title="myLogBook" />
        <Input
          textChange={todoInput => todoInput}
          addNewLog={() => this.addNewLog}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container:{
    flex: 1,
  }
});

export default App;

如果您想在代码中使用状态,您可以像这样使用 class 组件:

import React, { Component } from 'react';
import {View, Text, StyleSheet, Button} from 'react-native';
import Header from './components/Header';
import Input from './components/Input';

class App extends React.Component{
  constructor(props){
    super(props);

    this.state = {
      todoInput: '',
      todos:[
        {id: 0, title: 'sample1', done: false},
      ]
    }
  }

   addNewLog=() => {
    console.log(this.state.todos);
  }

  render() {
    return (
      <View style={styles.container}>
        <Header title="myLogBook" />
        <Input
          textChange={todoInput => todoInput}
          addNewLog={() => this.addNewLog}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container:{
    flex: 1,
  }
});

export default App;

在您的代码中,您在使用 ; 而不是 ,

时犯了一个错误

todoInput: '';更改为todoInput: '',

希望对您有所帮助!