로메오의 블로그

[React Native] scrollview 본문

Frontend/React

[React Native] scrollview

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

React 목록

 

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

interface State {
  name: string;
}

export default function App() {
  const [companies, setCompany] = useState<State[]>([
    {name: 'google'},
    {name: 'samsung'},
    {name: 'microsoft'},
    {name: 'facebook'},
    {name: 'apple'},
  ]);

  const add = () => {
    const newCompany = {name: 'newCompany'};
    setCompany(prevArray => [...prevArray, newCompany]);
  };
  const remove = (name: string) => {
    setCompany(prevArray => prevArray.filter(item => item.name !== name));
  };

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Company</Text>
      <ScrollView>
        {companies.map((company, index) => {
          return (
            <View style={styles.text} key={index}>
              <Text>{company.name}</Text>
              <Button title="삭제" onPress={() => remove(company.name)} />
            </View>
          );
        })}
        <Button title="추가" onPress={add} />
      </ScrollView>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop: 50,
  },
  title: {
    fontSize: 20,
    padding: 10,
  },
  text: {
    backgroundColor: '#eee',
    padding: 10,
    margin: 10,
  },
});

 

 

React 목록

반응형

'Frontend > React' 카테고리의 다른 글

[React Native] custom font 설정  (0) 2022.10.17
[React Native] redux  (0) 2022.10.17
[React Native] useState  (0) 2022.10.13
[React Native] state  (0) 2022.10.13
[React Native] Tab navigation 사용하기 (2022년 기준)  (0) 2022.10.12
Comments