๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JavaScript/React-Native

[React-Native] ์ปดํฌ๋„ŒํŠธ ์Šคํƒ€์ผ๋ง

by soy๋ฏธ๋‹ˆ 2021. 11. 4.

 

 

 

 

 

๐Ÿ’ก ์ธ๋ผ์ธ(inline) ์Šคํƒ€์ผ๋ง

์ปดํฌ๋„ŒํŠธ์˜ ํƒœ๊ทธ์— ์ง์ ‘ ์Šคํƒ€์ผ์„ ์ ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

 

// src/App.js
import React from "react";
import { View, Text } from "react-native";

const App = () => {
  return (
    <View
      style={{
        flex: 1,
        backgroundColor: "#fff",
        alignItems: "center",
        justifyContent: "center",
      }}
    >
      <Text
        style={{
          padding: 10,
          fontSize: 26,
          fontWeight: "600",
          color: "black",
        }}
      >
        Inline Styling - Text
      </Text>
      <Text
        style={{
          padding: 10,
          fontSize: 26,
          fontWeight: "400",
          color: "red",
        }}
      >
        Inline Styling - Error
      </Text>
    </View>
  );
};

export default App;

 

 

 

 

๐Ÿ’ก ํด๋ž˜์Šค ์Šคํƒ€์ผ๋ง

์ปดํฌ๋„ŒํŠธ ์Šคํƒ€์ผ์— StyleSheet ์— ์ •์˜๋œ ์Šคํƒ€์ผ์„ ์ ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

 

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

const App = () => {
  return (
    <View style={styles.container}>
      <Text style={styles.text}>Inline Styling - Text</Text>
      <Text style={styles.error}>Inline Styling - Error</Text>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
  },
  text: {
    padding: 10,
    fontSize: 26,
    fontWeight: "600",
    color: "black",
  },
  error: {
    padding: 10,
    fontSize: 26,
    fontWeight: "400",
    color: "red",
  },
});
export default App;

 

 

 

 

๐Ÿ’ก ์Šคํƒ€์ผ ์—ฌ๋Ÿฌ ๊ฐœ ์ ์šฉ

์Šคํƒ€์ผ์„ ์—ฌ๋Ÿฌ ๊ฐœ ์ ์šฉํ•จ์œผ๋กœ์จ ์ค‘๋ณต๋˜๋Š” ์ฝ”๋“œ๋ฅผ ์ค„์ด๊ณ  ๊ณตํ†ต๋œ ์ฝ”๋“œ์˜ ๊ด€๋ฆฌ๊ฐ€ ํŽธํ•ด์ง„๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์Šคํƒ€์ผ์„ ์ ์šฉํ•  ๋•Œ ์ฃผ์˜ํ•  ์ ์€ ์ ์šฉํ•˜๋Š” ์Šคํƒ€์ผ์˜ ์ˆœ์„œ์ด๋‹ค.

 

๐Ÿ’ก ๋’ค์— ์˜ค๋Š” ์Šคํƒ€์ผ์ด ๋จผ์ € ์˜ค๋Š” ์Šคํƒ€์ผ์„ ๋ฎ๋Š”๋‹ค.

 

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

const App = () => {
  return (
    <View style={styles.container}>
      <Text style={[styles.text, { color: "green" }]}>
        Inline Styling - Text
      </Text>
      <Text style={[styles.text, styles.error]}>Inline Styling - Error</Text>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
  },
  text: {
    padding: 10,
    fontSize: 26,
    fontWeight: "600",
    color: "black",
  },
  error: {
    fontWeight: "400",
    color: "red",
  },
});
export default App;

 

 

 

๐Ÿ’ก ์™ธ๋ถ€ ์Šคํƒ€์ผ ์ ์šฉํ•˜๊ธฐ

 

// src/App.js

import React from "react";
import { View, Text } from "react-native";
// ์™ธ๋ถ€ ์Šคํƒ€์ผ import ํ•ด์„œ ์‚ฌ์šฉ
import { viewStyles, textStyles } from "./styles";

const App = () => {
  return (
    <View style={viewStyles.container}>
      <Text style={[textStyles.text, { color: "green" }]}>
        Inline Styling - Text
      </Text>
      <Text style={[textStyles.text, textStyles.error]}>
        Inline Styling - Error
      </Text>
    </View>
  );
};
export default App;

 

App ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์•„๋‹Œ ์™ธ๋ถ€ ํŒŒ์ผ์— ์ •์˜๋œ ์Šคํƒ€์ผ (viewStyles, textStyles) ์„ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜์˜€๋‹ค.

 

// src/styles.js

import { StyleSheet } from "react-native";

export const viewStyles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
  },
});

export const textStyles = StyleSheet.create({
  text: {
    padding: 10,
    fontSize: 26,
    fontWeight: "600",
    color: "black",
  },
  error: {
    fontWeight: "400",
    color: "red",
  },
});

 

 

 

 

๐Ÿ’ก Layout, flex ๋“ฑ ์Šคํƒ€์ผ ์†์„ฑ ์ ์šฉํ•˜๊ธฐ

 

// src/components/Layout.js

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

export const Header = () => {
  return (
    <View style={[styles.container, styles.header]}>
      <Text style={styles.text}>Header</Text>
    </View>
  );
};

export const Contents = () => {
  return (
    <View style={[styles.container, styles.contents]}>
      <Text style={styles.text}>Contents</Text>
    </View>
  );
};

export const Footer = () => {
  return (
    <View style={[styles.container, styles.footer]}>
      <Text style={styles.text}>Footer</Text>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    width: "100%",
    alignItems: "center",
    justifyContent: "center",
    height: 80,
  },
  header: {
    backgroundColor: "#f1c40f",
  },
  contents: {
    flex: 1,
    backgroundColor: "#1abc9c",
    height: 640,
  },
  footer: {
    backgroundColor: "#3498db",
  },
  text: {
    fontSize: 26,
  },
});

 

// src/App.js

import React from "react";
import { View, Text } from "react-native";
import { Header, Footer, Contents } from "./components/Layout";
import { viewStyles } from "./styles";

const App = () => {
  return (
    <View style={viewStyles.container}>
      <Header />
      <Contents />
      <Footer />
    </View>
  );
};
export default App;

 

 

 

์•ฑ ์‹คํ–‰ ํ™”๋ฉด

 

 

 

 

 

๐Ÿ’ก Platform ์„ ์ด์šฉํ•ด์„œ ios, android ์— ๊ทธ๋ฆผ์ž ์ ์šฉํ•˜๊ธฐ

 

// src/components/ShadowBox.js

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

export default () => {
  return <View style={styles.shadow}></View>;
};

const styles = StyleSheet.create({
  shadow: {
    backgroundColor: "#fff",
    width: 200,
    height: 200,
    ...Platform.select({
      ios: {
        shadowColor: "#000",
        shadowOffset: {
          width: 10,
          height: 10,
        },
        shadowOpacity: 0.5,
        shadowRadius: 10,
      },
      android: {
        elevation: 20,
      },
    }),
  },
});

 

// src/App.js

import React from "react";
import { View, Text } from "react-native";
import { viewStyles } from "./styles";
import ShadowBox from "./components/ShadowBox";

const App = () => {
  return (
    <View style={viewStyles.container}>
      <ShadowBox />
    </View>
  );
};
export default App;

 

 

 

์•ฑ ์‹คํ–‰ ํ™”๋ฉด

 

 

 

 

 

 

๐Ÿ’ก ์Šคํƒ€์ผ๋“œ ์ปดํฌ๋„ŒํŠธ

๋ฐฑํ‹ฑ(`)์„ ์‚ฌ์šฉํ•˜์—ฌ ๋งŒ๋“  ๋ฌธ์ž์—ด์„ ๋ถ™์ด๊ณ  ๊ทธ ์•ˆ์— ์Šคํƒ€์ผ์„ ์ง€์ •ํ•˜๋ฉด ๋œ๋‹ค.

 

// src/components/Button.js

import React from 'react';
import styled from 'styled-components/native';

const ButtonContainer = styled.TouchableOpacity`
  background-color: #9b59b6;
  border-radius: 15px;
  padding: 15px 40px;
  margin: 18px 0px;
  justify-content: center;
`;

const Title = styled.Text`
  font-size: 20px;
  font-weight: 600;
  color: #fff;
`;

const Button = (props) => {
  return (
    <ButtonContainer>
      <Title>{props.title}</Title>
    </ButtonContainer>
  );
};

export default Button;

 

// src/App.js

import React from "react";
import styled from "styled-components/native";
import Button from "./components/Button";

const Container = styled.View`
  flex: 1;
  background-color: #ffffff;
  align-items: center;
  justify-content: center;
`;

const App = () => {
  return (
    <Container>
      <Button title="Hanbit" />
      <Button title="React Native" />
    </Container>
  );
};

export default App;

 

 

 

์•ฑ ์‹คํ–‰ ํ™”๋ฉด

 

 

 

 

 

 

 

๐Ÿ’ก ์Šคํƒ€์ผ๋“œ ์ปดํฌ๋„ŒํŠธ - props ์ „๋‹ฌ

App.js ์—์„œ props ๋กœ color ์ „๋‹ฌ (borderColor)

 

// src/App.js

import React from "react";
import styled from "styled-components/native";
import Button from "./components/Button";
import Input from "./components/Input";

const Container = styled.View`
  flex: 1;
  background-color: #ffffff;
  align-items: center;
  justify-content: center;
`;

const App = () => {
  return (
    <Container>
      <Button title="Hanbit" />
      <Button title="React Native" />
      <Input borderColor="#3498db" />
      <Input borderColor="#9b59b6" />
    </Container>
  );
};

export default App;

 

// src/components/Input.js

import React from "react";
import styled from "styled-components/native";

const StyledInput = styled.TextInput.attrs((props) => ({
  placeholder: "Enter a text...",
  placeholderTextColor: props.borderColor,
}))`
  width: 200px;
  height: 60px;
  margin: 5px;
  padding: 10px;
  border-radius: 10px;
  border: 2px;
  border-color: ${props => props.borderColor};
  font-size: 24px;
`;

const Input = (props) => {
  return <StyledInput borderColor={props.borderColor} />;
};

export default Input;

 

App.js ์—์„œ ์ „๋‹ฌํ•œ borderColor ๊ฐ’์„ props ๋กœ ๋ฐ›์•„์„œ ์‚ฌ์šฉ

 

 

 

 

์•ฑ ์‹คํ–‰ ํ™”๋ฉด

 

 

 

 

 

 

 

 

 

 

 

 

 

๋ณธ ํฌ์ŠคํŒ…์€ ๋„์„œ [์ฒ˜์Œ ๋ฐฐ์šฐ๋Š” ๋ฆฌ์•กํŠธ ๋„ค์ดํ‹ฐ๋ธŒ(ํ•œ๋น›๋ฏธ๋””์–ด, ๊น€๋ฒ”์ค€ ์ง€์Œ)] ๋ฅผ ๋ณด๊ณ  ๊ณต๋ถ€ํ•˜๋ฉฐ ๊ธฐ๋กํ•˜์˜€์Šต๋‹ˆ๋‹ค.

'JavaScript > React-Native' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[React-Native] React Native ์—์„œ ์ปดํฌ๋„ŒํŠธ๋ž€  (0) 2021.11.03
[React-Native] React Native ์‹œ์ž‘ํ•˜๊ธฐ  (0) 2021.11.02

๋Œ“๊ธ€