๐ก ์ธ๋ผ์ธ(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 |
๋๊ธ