기록중

인스타그램 클론코딩 (react-native, firebase) 고민했던것

lian_is_clone 2021. 12. 27. 21:54

출처

https://www.youtube.com/watch?v=1hPgQWbWmEk&t=10870s 

 

오늘의 문제 사항은 3:53:00 부분에 

if(props.following.indexOf( props.route.params.uid ) > -1 ){
            setFollowing(true);
        }else{
            setFollowing(false);
        }

이 부분이 이였다. 트루가 절대 안나온다.

많이 짜증이나 forech로 확인해봤다.

props.following.forEach(element => {
            console.log(element)
            console.log(props.route.params.uid)
        });

 

{id: '6difKPatRIWJ7H9EDHe9UNtOicc2'}
So2pDRITizTJFvBrqBm7ol3FOW32

{id: '6difKPatRIWJ7H9EDHe9UNtOicc2'}
6difKPatRIWJ7H9EDHe9UNtOicc2

 

이렇게 나온다. 아 오브젝트랑 스트링이랑 달라서 그런가?

그런데 유툽에선 잘만된다...

 

그럼 오는 정보를 스트링에서 오브젝트로 바꾸면? 그럼 코드 수정이 너무 크다. 

 

찾아보니 indexOf 와 비슷한것들이 있다.

참조 : https://bbaktaeho-95.tistory.com/40

 

[Javascript/Array] find, findIndex, indexOf (배열 검색)

📚 find, findIndex, indexOf 자바스크립트 Array.prototype 배열에서 원하는 값 또는 식별자를 찾아내는 메서드 배열을 순차 반복 find 는 인자로 받은 판별 함수를 만족하는 첫 번째 요소를 반환 findIndex 는

bbaktaeho-95.tistory.com

여기 보니 find indexOf findIndex를 잘 설명해 주셧다. 

 

보자보자 하니 이거 findIndex가 나한태 맞는 함수인거 같은데?

 

indexOf처럼 찾지 못하면 -1을 반환하고 찾으면 바로 종료해 낭비도 사라지고

 

적용했더니 바로 된다. 

 

if(props.following.findIndex((e) => e.id === props.route.params.uid ) > -1 ){
            setFollowing(true);
        }else{
            setFollowing(false);
        }

감사합니다. ㅠㅠ 이걸로 10분 동안 로그를 뚫어져라 봤습니다.