Advertisement
Felanpro

React Native (state)

May 16th, 2023
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. import React, { useState } from 'react';
  2. import { StatusBar } from 'expo-status-bar';
  3. import { StyleSheet, Text, View, Button, Alert } from 'react-native';
  4.  
  5. const Cat = props => {
  6. const [isHungry, setIsHungry] = useState(true); // useState() here works as [getter, setter] and getter will change value..
  7. //..depending on what is done to setIsHungry
  8.  
  9. return (
  10. <>
  11.  
  12. <Text> I am {props.name}, and I am {isHungry ? 'hungry' : 'full'}!</Text>
  13. <Button
  14. onPress = {() => {
  15. setIsHungry(false);
  16. }}
  17. disabled = {!isHungry}
  18. title = {isHungry ? 'Pour me some milk, please!' : 'Thank you!'}
  19. />
  20.  
  21. </>
  22. );
  23. };
  24.  
  25. export default function App() {
  26. return (
  27. <View style = {styles.container}>
  28.  
  29. <Cat name = "morris"/>
  30. <Cat name = "frasse"/>
  31.  
  32. </View>
  33. );
  34. }
  35.  
  36. const styles = StyleSheet.create({
  37. container: {
  38. flex: 1,
  39. backgroundColor: '#fff',
  40. alignItems: 'center',
  41. justifyContent: 'center',
  42. },
  43. });
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement