react-native - 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'
}
}
);
解决方案
您可以使用 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>
);
}
推荐阅读
- c# - 如何从 DataList(A) 中的 DataList(B) 中获取选定索引?
- docker - Docker:无法使用docker-compose参数连接到容器下的mysql
- azure - 部署在 Azure Linux WebApp 上的 .netcore 2.1 MVC 中的通知
- mysql - 当您必须在许多查询中使用派生列时,创建表会更好吗?
- javascript - React Native 在新初始化的应用程序上找不到模块 ./index
- python - 为什么我的 AdaBoost 实现的错误没有下降?
- html - 显示:@media 中的 None 正在全局执行
- python - 不使用 IF 语句的 Python 作业问题
- php - 如何在codeigniter中将“多选”值发布到数据表中
- java - jar 运行但立即退出