3월 협업 프로젝트(1석 4조) 👨‍👩‍👧‍👦

글 작성 페이지 장소 Daum 우편번호

엄성준 2023. 4. 5. 01:26

$ npm install react-daum-postcode 을 통해서 다음 우편번호 라이브러리를 설치해 줍니다.

 

import DaumPostcode from 'react-daum-postcode' 이 문장을 import 해줍니다.

 

import React from 'react'
import {useState} from 'react'
import PageTitle from '../Common/PageTitle'
import styled from 'styled-components'
import ThemeSlide from '../Common/ThemeSlide'
import DaumPostcode from 'react-daum-postcode'

import {FaMapMarkerAlt} from 'react-icons/fa'

export default function PostWritePage() {
  const [address, setAddress] = useState('')
  const [isOpenPost, setIsOpenPost] = useState(false)
  const onChangeOpenPost = () => {
    setIsOpenPost(!isOpenPost)
  }

  const onCompletePost = (data: any) => {
    let addr = data.address

    setAddress(addr)
    setIsOpenPost(false)
  }

  return (
    <>
      <PageTitle title='Writing post' sub='나의 여행 경험을 다른 사람들에게 들려주세요.' />
      <Section>
        <Title># 테마</Title>
        <ThemeSlide />
        <ContentBox>
          <Title># 제목</Title>
          <TitleInput type='text' maxLength={20} placeholder='# 제목을 입력하세요 (최대 20자)' required />
        </ContentBox>
        <ContentBox>
          <Title># 주소</Title>
          <AddresBox>
            <AddresInput type='text' value={address} placeholder='# 주소' required readOnly />
            <FaMapMarkerAlt onClick={onChangeOpenPost} />
            {isOpenPost ? (
              <div>
                <DaumPostcode
                  style={{
                    position: 'absolute',
                    marginTop: '0.2rem',
                    borderRadius: '0.5rem',
                    width: '25rem',
                    height: '29rem',
                    display: 'block',
                    padding: '0.5rem',
                    border: '2px solid #cacaca',
                  }}
                  autoClose
                  onComplete={onCompletePost}
                />
              </div>
            ) : null}
          </AddresBox>
        </ContentBox>
      </Section>
    </>
  )
}

const Section = styled.section`
  display: flex;
  flex-direction: column;
  padding: 0px 20rem 5rem;
`

const ContentBox = styled.div`
  display: flex;
  flex-direction: column;
  margin-top: 5rem;
`

const Title = styled.h2`
  font-size: 1.5rem;
  margin-right: 2rem;
`

const TitleInput = styled.input`
  border: none;
  border-bottom: 1px solid #c0c0c0;
  flex: 1 1 0;
  font-size: 1.5rem;
  width: 25%;
  margin-top: 3rem;

  &:focus {
    outline: none;
    border-bottom: 2px solid #6b7280;
  }
`

const AddresBox = styled.div`
  svg {
    font-size: 1.8rem;
    margin-left: 1rem;

    &:hover {
      cursor: pointer;
      color: #1877f2;
    }
  }
`

const AddresInput = styled.input`
  border: none;
  border-bottom: 1px solid #c0c0c0;
  flex: 1 1 0;
  font-size: 1.5rem;
  width: 25%;
  margin-top: 3rem;
  &:focus {
    outline: none;
  }
`

여기서 주의할 점은 <DaumPostcode/> component를 <div></div>로 감싸주지 않으면 아래와 같은 오류가 발생합니다.

 

React DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node

 

그럼 왜 div로 감싸줘야 하나 궁금하실 텐데 저는 <DaumPostcode/> 컴포넌트가 autoClose기능으로 DOM에서 삭제될 때 Node Child가 아니기 때문에 오류가 나는 것 같다라고 결론 내리게 되었습니다.