// App.js
import React, { useState } from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';
import axios from 'axios';
const App = () => {
const [joke, setJoke] = useState('');
const fetchJoke = async () => {
try {
const response = await axios.get('https://official-joke-api.appspot.com/random_joke');
setJoke(`${response.data.setup} - ${response.data.punchline}`);
} catch (error) {
console.error(error);
setJoke('Failed to fetch joke');
}
};
return (
<View style={styles.container}>
<Text style={styles.header}>Random Joke Generator</Text>
<Text style={styles.joke}>{joke}</Text>
<Button title="Get Joke" onPress={fetchJoke} />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff',
},
header: {
fontSize: 24,
marginBottom: 20,
},
joke: {
fontSize: 18,
textAlign: 'center',
marginBottom: 20,
},
});
export default App;const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff',
},
header: {
fontSize: 24,
marginBottom: 20,
},
joke: {
fontSize: 18,
textAlign: 'center',
marginBottom: 20,
},
});
export default App;const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff',
},
header: {
fontSize: 24,
marginBottom: 20,
},
joke: {
fontSize: 18,
textAlign: 'center',
marginBottom: 20,
},
});
export default App;const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff',
},
header: {
fontSize: 24,
marginBottom: 20,
},
joke: {
fontSize: 18,
textAlign: 'center',
marginBottom: 20,
},
});
export default App;
11. 基于手机APP开发实例--随机笑话生成器
原创mb64cc5144d532c ©著作权
©著作权归作者所有:来自51CTO博客作者mb64cc5144d532c的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
随机号码生成器
随机生成数字
bash