Typescript 意外标记,应为'';在本机构造函数中

Typescript 意外标记,应为'';在本机构造函数中,typescript,react-native,Typescript,React Native,当我想在我的React原生项目中添加构造函数时,我总是以意外的标记结束,预期“;”(6:28) 我已经尝试在每个可能的位置添加分号,但无法找出实际问题所在,因为我不熟悉TypeScript和React Native export default function App() { constructor(props: Props) { super(props); this.state = { welcomeText: 'Hey this is not worki

当我想在我的React原生项目中添加构造函数时,我总是以意外的标记结束,预期“;”(6:28)

我已经尝试在每个可能的位置添加分号,但无法找出实际问题所在,因为我不熟悉TypeScript和React Native

export default function App() {

  constructor(props: Props) {
    super(props);

    this.state = {
      welcomeText: 'Hey this is not working'
    };
  }

  return (
    <View style={styles.container}>
      <View style={styles.subContainer}>

      </View>
      <View style={styles.subContainer}>
        <Text style={text.header}>Welcome</Text>
        <Text style={text.onBoardingContent}>

        </Text>
      </View>
    </View>
  );
}

...

您正在使用一个功能组件。函数在es6中没有构造函数。您应该改用类组件。它看起来像这样:

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { myState: false };
  }
render() {
      return <h1>Class component.</h1>;
    }
}
类应用程序扩展了React.Component{
建造师(道具){
超级(道具);
this.state={myState:false};
}
render(){
返回类组件。;
}
}

Um
export default class App{
您正在将一个类与一个函数混合,
export default class App{constructor(props:props){..}render(){return(…)}}
class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { myState: false };
  }
render() {
      return <h1>Class component.</h1>;
    }
}