Skip to content

Commit 08f5d57

Browse files
authored
Merge pull request #61 from HK1211/fix-53
fixed: 깃헙이슈_#53: DaumpostcodeEmbed 메소드 사용시 Document.write() 가문제가 됩니다.
2 parents f0f9ee1 + d8d296e commit 08f5d57

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

src/DaumPostcodeEmbed.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ const defaultProps = {
4242
class DaumPostcodeEmbed extends Component<DaumPostcodeEmbedProps, State> {
4343
static defaultProps = defaultProps;
4444

45+
/**
46+
* 1. 깃헙이슈_#53: DaumpostcodeEmbed 메소드 사용시 Document.write() 가 문제가 됩니다.
47+
* 2. 에러메세지: caught TypeError: Cannot read properties of null (reading 'document').
48+
* 3. 원인: `loadPostcode` 함수가 두번 실행되어 발생하는 문제입니다.
49+
* * React StrictMode에서는 의도적으로 라이프사이클 메소드가 두 번 호출되게 설계되어 있습니다.
50+
* * 이는 개발 과정에서 부작용(side-effects)이 발생하는 것을 감지하고 문제를 미리 찾아낼 수 있도록 하는 목적이 있습니다.
51+
* 4. 해결: 의도적으로 componentDidMount시 `loadPostcode` 함수가 1번만 호출하도록 변수 `mount`를 추가하여, mount값이 false 일때만 `loadPostcode`를 실행하면 해결됩니다.
52+
* @see =================== 변경 내역 ==================
53+
* [작성자][작업일시] - 내용
54+
* [HK1211][2023-05-28 Sunday 00:48:08] - 최초작성
55+
*/
56+
private mounted = false;
57+
4558
wrap = createRef<HTMLDivElement>();
4659

4760
state = {
@@ -53,7 +66,10 @@ class DaumPostcodeEmbed extends Component<DaumPostcodeEmbedProps, State> {
5366
const { scriptUrl } = this.props;
5467

5568
if (!scriptUrl) return;
56-
loadPostcode(scriptUrl).then(initiate).catch(onError);
69+
if (!this.mounted) {
70+
loadPostcode(scriptUrl).then(initiate).catch(onError);
71+
this.mounted = true; // react@18 부터 development 환경에서 React.StrictMode 사용시 출력되는 문제해결.
72+
}
5773
}
5874

5975
initiate = (Postcode: PostcodeConstructor) => {

0 commit comments

Comments
 (0)