首页 > 解决方案 > React-Native 构造函数

问题描述

请帮我找出以下 React-Native 代码有什么问题?

它说在构造函数(道具)之后应该有';' 分号。我不知道我是否以正确的方式声明了它。

import React from 'react';
import { StyleSheet, TextInput, View } from 'react-native';

export default function App() {
  constructor (props){
    this.state = {
      text: 'HI'
    }
  }
  render () {
  return (
    <View style={styles.container}>
      <TextInput style={styles.input}
      placeholder = 'Enter Value...'
      placeholderTextColor ='#E74292'
      onChangeText = {(text) => {
        this.setState({text})
      }}
      />
    </View>
  );
}
const styles = StyleSheet.create({
  container:{
    flex:1,
    backgroundColor:'#F4C724',

  },
  input :{
marginTop:30,
height:30,
width:30,
borderWidth:2,
padding:10,
borderRadius: 5,
borderColor:'#1287A5'
  }
}
);

标签: react-native

解决方案


您可以使用 useState ,动态函数值在加载时加载到初始变量

import React,{ useState } from 'react';
import { View, Text, Button, ImageBackground} from 'react-native';
export default function Home({navigation}){

   //function getversion onload 

    const [initval,setInitval] = useState(()=>{ return '123456'});

    return(
        <View  style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text> {initval} </Text>
        </View>
    );
}

推荐阅读