1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

メモアプリのホーム画面をコンポーネント化する

1
Last updated at Posted at 2021-11-26

コンポーネント化

フォルダを新規作成

プロジェクトの直下にsrcフォルダを作成する。
srcフォルダ直下にcomponentsフォルダを作成する。

appbarをコンポーネント化

components直下に、AppBar.jsxファイルを作成する。
App.jsxから移植する。

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

// eslint-disable-next-line react/function-component-definition
export default function AppBar() {
  return (
    <View style={styles.appbar}>
      <View style={styles.appbarInner}>
        <Text style={styles.appbarTitle}>MEMO</Text>
        <Text style={styles.appbarLogout}>ログアウト</Text>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  appbar: {
    width: '100%',
    height: 110,
    backgroundColor: '#C51162',
    justifyContent: 'flex-end',
  },
  appbarInner: {
    alignItems: 'center',
  },
  appbarTitle: {
    color: '#fff',
    fontSize: 24,
    lineHeight: 32,
    fontWeight: 'bold',
    marginBottom: 8,
  },
  appbarLogout: {
    position: 'absolute',
    right: 20,
    bottom: 12,
    color: 'rgba(255,255,255,0.7)',
  },
});

メモのアイテムリストをコンポーネント化

components直下にMemoList.jsxを作成する。
App.jsxから移植してくる。

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

// eslint-disable-next-line react/function-component-definition
export default function MemoList() {
  return (
    <View>

      <View style={styles.memoItem}>
        <View>
          <Text style={styles.memoItemTitle}>買い物</Text>
          <Text style={styles.memoItemDate}>2020年12月22日 10:00</Text>
        </View>
        <View>
          <Text>x</Text>
        </View>
      </View>

      <View style={styles.memoItem}>
        <View>
          <Text style={styles.memoItemTitle}>買い物2</Text>
          <Text style={styles.memoItemDate}>2020年12月22日 10:00</Text>
        </View>
        <View>
          <Text>x</Text>
        </View>
      </View>

    </View>
  );
}

const styles = StyleSheet.create({
  memoItem: {
    backgroundColor: '#fff',
    flexDirection: 'row',
    justifyContent: 'space-between',
    paddingVertical: 16,
    paddingHorizontal: 19,
    alignItems: 'center',
    borderBottomWidth: 1,
    borderColor: 'rgba(0,0,0,0.20)',
  },
  memoItemTitle: {
    fontSize: 16,
    lineHeight: 32,
  },
  memoItemDate: {
    fontSize: 12,
    lineHeight: 16,
    color: '#616161',
  },
});

新規追加ボタンをコンポーネント化

compontents直下にCircleButton.jsxを作成する。
App.jsxから移植してくる。

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

// eslint-disable-next-line react/function-component-definition
export default function CircleButton() {
  return (
    <View style={styles.circleButton}>
      <Text style={styles.circleButtonLabel}>+</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  circleButton: {
    backgroundColor: '#C51162',
    width: 65,
    height: 65,
    borderRadius: 32,
    justifyContent: 'center',
    alignItems: 'center',
    position: 'absolute',
    right: 40,
    bottom: 40,
    /* shadowはiphoneにしか効かないCSS */
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 8 },
    shadowOpacity: 0.35,
    shadowRadius: 5,
    /* elevationはAndroid用のCSS */
    elevation: 8,
  },
  circleButtonLabel: {
    color: '#fff',
    fontSize: 32,
    lineHeight: 32,
    fontWeight: 'bold',
  },
});

App.jsxにコンポーネントを読み込む

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

import AppBar from './src/components/AppBar';
import CircleButton from './src/components/CircleButton';
import MemoList from './src/components/MemoList';

// eslint-disable-next-line react/function-component-definition
export default function App() {
  return (
    <View style={styles.container}>
      <AppBar />
      <MemoList />
      <CircleButton />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#FCE4EC',
  },
});

App.jsxがスッキリした。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?