aakash2310

Untitled

Mar 27th, 2023 (edited)
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. import React, { useState } from 'react';
  2. import { Button, StyleSheet, Text, View } from 'react-native';
  3.  
  4. const App = () => {
  5. const [joke, setJoke] = useState('');
  6.  
  7. const getJoke = async () => {
  8. try {
  9. const response = await fetch('https://v2.jokeapi.dev/joke/Any?type=single');
  10. const data = await response.json();
  11. setJoke(data.joke);
  12. } catch (error) {
  13. console.error(error);
  14. }
  15. };
  16.  
  17. return (
  18. <View style={styles.container}>
  19. <Button title="Get Joke" onPress={getJoke} />
  20. <Text style={styles.jokeText}>{joke}</Text>
  21. </View>
  22. );
  23. };
  24.  
  25. const styles = StyleSheet.create({
  26. container: {
  27. flex: 1,
  28. justifyContent: 'center',
  29. alignItems: 'center',
  30. backgroundColor: '#fff',
  31. },
  32. jokeText: {
  33. marginTop: 20,
  34. fontSize: 20,
  35. textAlign: 'center',
  36. },
  37. });
  38.  
  39. export default App;
Advertisement
Add Comment
Please, Sign In to add comment