로메오의 블로그

[React Native] useState 본문

Frontend/React

[React Native] useState

romeoh 2022. 10. 13. 16:29
반응형

React 목록

 

import React, {useState} from 'react';
import {View, Text, StyleSheet, Button} from 'react-native';

interface State {
  age: number;
}

export default function App() {
  const [count, setCount] = useState<State>({age: 0});

  const increase = () => {
    setCount({age: count.age + 1});
  };
  const descease = () => {
    setCount({age: count.age - 1});
  };

  return (
    <View style={styles.container}>
      <Text>{count.age}</Text>
      <Button title="+1" onPress={increase} />
      <Button title="-1" onPress={descease} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    alignContent: 'center',
  },
});

 

 

React 목록

반응형
Comments