1 回答
TA贡献1817条经验 获得超14个赞
从导入中删除 Date,Date 内置于 React Native,您不需要导入它。对您的消息使用挂钩(useState 和 useEffect)并返回一个 Text 组件。
import React from 'react';
import { StyleSheet, Text, View, Image } from 'react-native';
import { StatusBar } from 'expo-status-bar';
const Greeting = (props) => {
const [message, setMessage] = React.useState('Evening');
React.useEffect(() => {
const now = new Date().getHours();
if (now < 12) {
setMessage('Morning');
} else if (now >= 12 && now <= 17) {
setMessage('Afternoon');
}
}, []);
return <Text style={styles.h1}>{message}</Text>;
};
const styles = StyleSheet.create({
h1: {
color: 'black',
backgroundColor: '#f9f9f9',
},
});
export default Greeting;
添加回答
举报