웹브라우저 CSS 활용.
Box.js 만들기
import React from 'react';
import {View, StyleSheet} from 'react-native';
function Box() {
return <View style={styles.box} />;
}
const styles = StyleSheet.create({
box: {
width: 64,
heigth: 64,
backgroundColor: 'black',
},
});
export default Box;
자꾸 코딩 줄맞춤때문에 애러나서 빡쳐서 pretiier 끔
import React from 'react';
import { SafeAreaView } from 'react-native';
import Box from './componets/Box';
const App = () => {
return (
<SafeAreaView>
<Box />
</SafeAreaView>
);
};
export default App;
기본 포맷이나 안됨...자꾸 애러남...
크기 설정
import React from 'react';
import {View, StyleSheet} from 'react-native';
function Box(props) {
return (
<View
style={[styles.box, props.rounded && styles.rounded, sizes[props.size]]}
/>
);
}
Box.defaultProps = {
size: 'medium',
};
const styles = StyleSheet.create({
box: {
backgroundColor: 'black',
},
rounded: {
borderRadius: 16,
},
small: {
width: 32,
height: 32,
},
medium: {
width: 64,
height: 64,
},
large: {
width: 128,
height: 128,
},
});
const sizes = {
small: styles.small,
medium: styles.medium,
large: styles.large,
};
export default Box;
App.js 수정
import React from 'react';
import {SafeAreaView} from 'react-native';
import Box from './componets/Box';
const App = () => {
return (
<SafeAreaView>
<Box rounded={true} size="large" />
</SafeAreaView>
);
};
export default App;
size small,medium,large로 바꾸면 잘됨.
box.js props개체 구조 분해 할당하면 아래와 같음
import React from 'react';
import {View, StyleSheet} from 'react-native';
function Box({rounded, size, color}) {
return (
<View
style={[
styles.box,
rounded && styles.rounded,
sizes[size],
{
backgroundColor: color,
},
]}
/>
);
}
Box.defaultProps = {
size: 'medium',
color: 'balck',
};
const styles = StyleSheet.create({
box: {
backgroundColor: 'yellow',
},
rounded: {
borderRadius: 16,
},
small: {
width: 32,
height: 32,
},
medium: {
width: 64,
height: 64,
},
large: {
width: 128,
height: 128,
},
});
const sizes = {
small: styles.small,
medium: styles.medium,
large: styles.large,
};
export default Box;
잘됨