<๊ฐ์>
์น๊ตฌList๋ฅผ ํ์ดํ ํค๋ฅผ ๋๋ฌ ํ ๊ธ ์ํฌ ๊ฒ์ด๋ค.
ํ ๊ธ ์ํฌ๋ ค๋ฉด boolean ๋ณ์๋ฅผ ๋ง๋ค๊ณ , ์ฐ๋ฆฌ๊ฐ ์ํฉ์ ๋ฐ๋ผ ๊ทธ ๊ฐ์ ์ํ๋ฅผ ๋ณํ ์์ผ์ฃผ๋ฉด ๋๋ค. (useState ์ด์ฉ)
๋ณํ๊ฐ ํ์ํ ๊ณณ์ friendSection, friendList 2๊ฐ์ง ์ฅ์์ด๋ค.
boolean ๋ณ์๊ฐ false ์ผ๋, friendSection์ ํ์ดํ๋ฅผ ์๋ก ๋ณด๊ฒ ํ๊ณ , true ์ผ๋๋ ์๋๋ฅผ ๋ณด๊ฒ ๋ง๋ค์ด์ผ ํ๋ค.
friednList์์ boolean ๋ณ์๊ฐ false ์ผ ๋, return ๊ฐ์ null๋ก ์ค์ ์ ๋ณด์ด๊ฒ ๋ง๋ค๊ณ true์ผ ๋ ์๋ ๋ก์ง์ ๋ณด์ด๊ฒ ๋ง๋ค๋ฉด ๋๋ค. ์ฌ๊ธฐ์ ์ฐ์ด๋ ๊ฒ์ด ์ผํญ ์ฐ์ฐ์ ํน์ if๋ฌธ, &&๋ฌธ ์ด๋ค.
1. ์ฝ๋ ๋ฆฌ๋ทฐ
(1) App.js
// useState๋ก boolean ๋ณ์์ ์ํ ๋ณํ ๋ํ๋ด๊ธฐ.
// boolean ๋ณ์๋ ์ฐธ/๊ฑฐ์ง ๋ฐ์ ์์ด์ toggle button ๋ง๋ค๊ธฐ์ ์ ํฉ
const [isOpened, setIsOpened] =useState(true);
// ํด๋ฆญ ์ ๋ก์ง ๋ง๋ค๊ธฐ
const onPressArrow = () => {
console.log('clicked arrow');
// ํ์ฌ ์ํ์์ ๋ฐ๋๊ฐ์ผ๋ก ํ ๊ธ
setIsOpened(!isOpened);
};
// ์ฐ๋ฆฌ๊ฐ ๋ง๋ boolean toggle ๋๋ ๋ณ์๋ฅผ ๋ฃ์ด์ค์ผ ํ๋ ํํธ
<HomelessSection
idCardLen ={idCard.length}
onPressArrow = {onPressArrow}
isOpened ={isOpened}
/>
<HomelessList data = {idCard} isOpened={isOpened}/>
(2) HomelessSection.js (์น๊ตฌ ์์ toggle ๋ฒํผ ์๋ ๊ณณ)
export default (props) => {
return (
<View style={{flexDirection: "row", justifyContent: "space-between"}}>
<Text style={{fontSize:15, color: "grey"}}> ์ผ์น์จ 90% ์ด์ : {props.idCardLen}</Text>
// ํด๋ฆญ์ toggle ๋จ.
<TouchableOpacity onPress={props.onPressArrow}>
// boolean ๋ณ์๊ฐ ์ฐธ์ด๋ ๊ฑฐ์ง์ด๋ ๋ฐ๋ผ ์์ด์ฝ ๋ฌ๋ผ์ง.
<MaterialIcons name={props.isOpened ? "keyboard-arrow-down" : "keyboard-arrow-up"} size={24} color="grey" />
</TouchableOpacity>
</View>
)
}
(3) HomelessList.js (์น๊ตฌ ๋ฆฌ์คํธ ๋ฐฐ์ด์ด ์๋ ๊ณณ)
isOpened๊ฐ false ์ด๋ฉด ์ปดํฌ๋ํธ์ return ๊ฐ์ null ๋ก ๋ฐ๊พผ๋ค.
isOpened๊ฐ true ์ด๋ฉด ์ปดํฌ๋ํธ์ return ๊ฐ์ ์๋๋๋ก ๋๋๋ค.
์ด๋ฅผ ๊ตฌํํ๊ธฐ ์ํ Logic์ ์ด 3๊ฐ์ง๊ฐ ์๋ค.
a. ์ผํญ ์ฐ์ฐ์ ์ฌ์ฉ
/*
* case 1
* ์ผํญ ์ฐ์ฐ์ ์ฌ์ฉ
* */
return props.isOpened ? (
<ScrollView contentContainerStyle ={{paddingBottom: bottomSpace}}>
{props.data.map((item, index) => {
return(
<View key={index}>
<CandidateProfile
name = {item.name}
uri = {item.uri}
location = {item.location}
date = {item.date}
persent = {item.persent}
/>
<Margin top = {3}/>
</View>
)
})}
</ScrollView>
) : null;
b. if ๋ฌธ ์ฌ์ฉ
(์ถ์ฒ! ํ๋ ค๋ ๊ฒ์ด ๋ฌด์์ธ์ง ๋จธ๋ฆฟ๊ธ์์ ๋ฐ๋ก ๋ณด์ด๋ฏ๋ก ๊ฐ๋ ์ฑ์ด ๋ฐฉ๋ฒ๋ค ์ค ์ต๊ณ ๋๋ค.)
/**
* 2. if ๋ฌธ์ผ๋ก ์์ธ ์ฒ๋ฆฌ ์์์ ๋ฌด์จ ์ด์ผ๊ธด์ง ๋ฐ๋ก ์์์ ๊ฐ๋
์ฑ์ด ์ข์.
*/
if(!props.isOpened) return null;
return(
<ScrollView contentContainerStyle ={{paddingBottom: bottomSpace}}>
{props.data.map((item, index) => {
return (
<View key={index}>
<CandidateProfile
name = {item.name}
uri = {item.uri}
location = {item.location}
date = {item.date}
persent = {item.persent}
/>
<Margin top = {3}/>
</View>
)
})}
</ScrollView>
)
c. &&๋ฌธ ์ฌ์ฉ
ํํ: ๋ณ์ &&(์คํํ ๋ฌธ์ฅ)
๋ณ์๊ฐ false์ด๋ฉด ์คํํ ๋ฌธ์ฅ ์ ์ฝ๊ณ ๋ฐ๋ก false ๋ฐํ, ๋ณ์๊ฐ true์ด๋ฉด ๊ทธ์ ์์ผ ์คํํ ๋ฌธ์ฅ ์ฝ๊ณ ์ํ
/**
* &&๋ฌธ ์ฌ์ฉ
* ์ฒซ๋ฒ์งธ ์ธ์๊ฐ false ์ด๋ฉด ๊ทธ๋๋ก false ๊ฐ ๋ฆฌํด (&& ๋ค์ ๋ฌธ์ฅ์ check ์ํจ), true์ด๋ฉด && ๋ค์ ๋ฌธ์ฅ ์คํ
*/
return props.isOpened && (
<ScrollView contentContainerStyle ={{paddingBottom: bottomSpace}}>
{props.data.map((item, index) => {
return(
<View key={index}>
<CandidateProfile
name = {item.name}
uri = {item.uri}
location = {item.location}
date = {item.date}
persent = {item.persent}
/>
<Margin top = {3}/>
</View>
)
})}
</ScrollView>
)
2. ์ค์ค๋ก ํด๋ณด๊ธฐ
๋ฐฐ์ด ๊ฒ. Hook์ ์ฌ์ฉํ๋ ค๋ ์ปดํฌ๋ํธ ์์์ ์ ์ธ ํด์ผํ๋ค.
const statusBarHeight = getStatusBarHeight(true);
const bottomSpace = getBottomSpace();
export default function App() {
const [isOpened, setIsOpened]=useState(true);
const onPressArrow = () => {
console.log("clicked arrow")
setIsOpened(!isOpened)
}
return (
<View style={styles.container}>
<Header />
<Margin height = {10}/>
<Profile
uri={myProfile.uri}
name={myProfile.name}
introduction={myProfile.introduction}
/>
<Margin height={15}/>
<Division/>
<Margin height={12}/>
<FriendSection
friendProfileLen = {friendProfiles.length}
onPressArrow={onPressArrow}
isOpened = {isOpened}
/>
<FriendList
data = {friendProfiles}
isOpened = {isOpened}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
paddingTop: statusBarHeight,
paddingHorizontal: 15,
},
});
import { ScrollView, View } from "react-native"
import { getBottomSpace } from "react-native-iphone-x-helper"
import Margin from "./Margin";
import Profile from "./Profile"
const bottomSpace =getBottomSpace();
export default (props) => {
if(!props.isOpened) {return null}
return(
<ScrollView showsVerticalScrollIndicator={false} contentContainerStyle={{paddingBottom: bottomSpace}}>
{props.data.map((item, index) => (
<View key={index}>
<Profile
uri={item.uri}
name={item.name}
introduction={item.introduction}
/>
<Margin height={13}/>
</View>
))}
</ScrollView>
)
}
import { View, Text, TouchableOpacity } from "react-native"
import { MaterialIcons } from '@expo/vector-icons';
export default (props) => {
return (
<View style={{flexDirection: "row", justifyContent: "space-between"}}>
<Text style={{color: "grey"}}> ์น๊ตฌ {props.friendProfileLen}</Text>
<TouchableOpacity onPress={props.onPressArrow}>
<MaterialIcons name={props.isOpened ? "keyboard-arrow-down" : "keyboard-arrow-up"} size={24} color="lightgrey" />
</TouchableOpacity>
</View>
)
}