Showing
4 changed files
with
18 additions
and
20 deletions
1 | import React, { useEffect } from "react"; | 1 | import React, { useEffect } from "react"; |
2 | import "./kakaoBtn.scss"; | 2 | import "./kakaoBtn.scss"; |
3 | import kakao from "../../assets/kakao.png"; | 3 | import kakao from "../../assets/kakao.png"; |
4 | +import useScript from "../../hooks/useScript"; | ||
4 | 5 | ||
5 | const KakaoBtn = ({result}) => { | 6 | const KakaoBtn = ({result}) => { |
7 | + const { loaded } = useScript("https://developers.kakao.com/sdk/js/kakao.js"); | ||
8 | + | ||
6 | useEffect(()=>{ | 9 | useEffect(()=>{ |
7 | - createKakaoButton(); | 10 | + if(loaded) |
8 | - },[]); | 11 | + createKakaoButton(); |
12 | + },[loaded]); | ||
9 | const createKakaoButton = () => { | 13 | const createKakaoButton = () => { |
10 | // kakao sdk script이 정상적으로 불러와졌으면 window.Kakao로 접근이 가능합니다 | 14 | // kakao sdk script이 정상적으로 불러와졌으면 window.Kakao로 접근이 가능합니다 |
11 | if (window.Kakao) { | 15 | if (window.Kakao) { |
12 | const kakao = window.Kakao; | 16 | const kakao = window.Kakao; |
13 | // 중복 initialization 방지 | 17 | // 중복 initialization 방지 |
18 | + | ||
14 | if (!kakao.isInitialized()) { | 19 | if (!kakao.isInitialized()) { |
15 | // 두번째 step 에서 가져온 javascript key 를 이용하여 initialize | 20 | // 두번째 step 에서 가져온 javascript key 를 이용하여 initialize |
16 | kakao.init(process.env.REACT_APP_KAKAO_KEY); | 21 | kakao.init(process.env.REACT_APP_KAKAO_KEY); |
... | @@ -53,4 +58,4 @@ const KakaoBtn = ({result}) => { | ... | @@ -53,4 +58,4 @@ const KakaoBtn = ({result}) => { |
53 | </div> | 58 | </div> |
54 | ); | 59 | ); |
55 | }; | 60 | }; |
56 | -export default KakaoBtn; | 61 | +export default KakaoBtn; |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
src/containers/KakaoBtnContainer.js
deleted
100644 → 0
1 | -import React,{useEffect} from "react"; | ||
2 | -import useScript from "../hooks/useScript"; | ||
3 | -import KakaoBtn from "../components/kakao/KakaoBtn"; | ||
4 | - | ||
5 | -const KakaoBtnContainer = ({result}) => { | ||
6 | - useScript("https://developers.kakao.com/sdk/js/kakao.js"); | ||
7 | - | ||
8 | - return ( | ||
9 | - <div className="layout"> | ||
10 | - <KakaoBtn result={result} /> | ||
11 | - </div> | ||
12 | - ); | ||
13 | -}; | ||
14 | -export default KakaoBtnContainer; |
1 | -import { useEffect } from "react"; | 1 | +import { useEffect, useState } from "react"; |
2 | 2 | ||
3 | const useScript = (url) => { | 3 | const useScript = (url) => { |
4 | + const [loaded, setLoaded] = useState(false); | ||
5 | + | ||
4 | useEffect(() => { | 6 | useEffect(() => { |
5 | const script = document.createElement("script"); | 7 | const script = document.createElement("script"); |
6 | script.src = url; | 8 | script.src = url; |
7 | script.async = true; | 9 | script.async = true; |
10 | + script.onload = () => { | ||
11 | + setLoaded(true); | ||
12 | + } | ||
8 | document.body.appendChild(script); | 13 | document.body.appendChild(script); |
9 | return () => { | 14 | return () => { |
10 | document.body.removeChild(script); | 15 | document.body.removeChild(script); |
11 | }; | 16 | }; |
12 | }, [url]); | 17 | }, [url]); |
18 | + | ||
19 | + return { loaded }; | ||
13 | }; | 20 | }; |
14 | -export default useScript; | 21 | +export default useScript; |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
1 | import React,{useEffect} from "react"; | 1 | import React,{useEffect} from "react"; |
2 | import styled, { css } from "styled-components"; | 2 | import styled, { css } from "styled-components"; |
3 | import { lighten,darken } from "polished"; | 3 | import { lighten,darken } from "polished"; |
4 | -import KakaoBtn from "../containers/KakaoBtnContainer"; | 4 | +import KakaoBtn from "../components/kakao/KakaoBtn"; |
5 | 5 | ||
6 | const sizes = { | 6 | const sizes = { |
7 | desktop: 102.4, | 7 | desktop: 102.4, | ... | ... |
-
Please register or login to post a comment