본문 바로가기
개발 일지/TIL

[ React ] 웹 링크 유효성 검사, React Query Firebase

by CODESIGN 2023. 1. 27.

웹 링크 유효성 검사


let regex = /^(http(s)?:\/\/)([^\/]*)(\.)(com|net|kr|my|shop)/gi;
const trueOrFalse = regex.test(input);
if (trueOrFalse === true) {
	setErrorMessage('');
	setInputValidation(true);
} else {
	setErrorMessage('유효한 주소를 적어주세요.');
	setInputValidation(false);
}

 

/: 정규식 시작

^: 문자열의 시작

(http(s)?:\/\/): http:// 또는 https:// 허용

([^\/]*): ^는 not의 의미로 /를 제외한 모든 문자열을 허용한다.

(\.): .를 선언해야 한다.

(com|net|kr|my|shop): 선언된 것만 허용한다.

/: 정규식 끝

g: 전역

i: 대소문자 구분 없음

 

 

React Query Firebase


React Query Firebase 쓰기 전

// github 수정
const docRef = doc(db, 'users', currentUser.uid);
const mutation = useFirestoreDocumentMutation(docRef);

const useSaveEdit = () => {
    const docRef = doc(db, 'users', currentUser.uid);
        if (docRef.converter === null) {
            await setDoc(docRef, {
            github: currentInput,
          });
          updateGithub(currentInput);
        } else {
            await updateDoc(docRef, {
            github: currentInput,
            });
            updateGithub(currentInput);
        }
}

 

React Query Firebase 쓴 후

import { useFirestoreDocumentMutation } from '@react-query-firebase/firestore';
const docRef = doc(db, 'users', currentUser.uid);
const mutation = useFirestoreDocumentMutation(docRef);

const useSaveEdit = () => {
	mutation.mutate({ github: currentInput });
	updateGithub(currentInput);
}

댓글