Durumis Inc.

translation

This is an AI translated post.

๋š ๋š ๋ฉ์˜ ์ƒ๊ฐ๋“ค

React - How to fix the 'kakao is not defined' error

Select Language

  • English
  • ๆฑ‰่ฏญ
  • Espaรฑol
  • Bahasa Indonesia
  • Portuguรชs
  • ะ ัƒััะบะธะน
  • ๆ—ฅๆœฌ่ชž
  • ํ•œ๊ตญ์–ด
  • Deutsch
  • Franรงais
  • Italiano
  • Tรผrkรงe
  • Tiแบฟng Viแป‡t
  • เน„เธ—เธข
  • Polski
  • Nederlands
  • เคนเคฟเคจเฅเคฆเฅ€
  • Magyar

Summarized by durumis AI

  • The problem that occurred when integrating the Kakao Map API in a React 18 environment with create-react-app, where the map was not rendered, is because React components do not directly access global objects.
  • React avoids direct access to global objects for encapsulation, performance, and state management, and uses centralized state management libraries like Redux to control data flow.
  • Unlike Vue, React gives developers more freedom in terms of folder structure and hooks, but it can be more challenging for novice developers. It's recommended to get familiar with it through toy projects.
  • Issue
  • Solution
  • Reason
  • Thoughts


Issue

While integrating Kakao Map API in a React 18, create-react-app environment, the map didn't render!

I had inserted the script tag within the head tag, so I thought it would load before the browser rendered.

** The HTML was written but it's not visible!!

~Roughly code that calls Kakao Map inside the head tag of index.html~

<!-- index.html -->
 
<head>
    <script type="text/javascript" src="//dapi.kakao.com/v2/maps/sdk.js?appkey=%REACT_APP_API_KEY%&libraries=services,clusterer,drawing" id="map-lib"></script>
useEffect(() => {
    // map ๋ถˆ๋Ÿฌ์˜ค๊ธฐ    


However, the console only recordedkakao is not defined,and nothing was rendered.


Solution

Basics: React components don't directly access global objects.


Solution 1-1

Declare the kakao property from the global object outside the component function.

const { kakao } = window;

function Map() {
    //...


Solution 1-2

Create separate custom hooks for the functions needed to render the Kakao Map.

//hooks/useKakaoMap.js

const { kakao } = window;

function useKakaoMap() {
    const initializeMap = useCallback(() => {
        const container = document.getElementById('map');
        const options = {
        center: new kakao.maps.LatLng(33.450701, 126.570667),
            level: 3
        };
        const newMap = new kakao.maps.Map(container, options);
        setMap(newMap);
    }, []);

    //...    
    
    return {
        initializeMap
    }
}

export default useKakaoMap;

//Map.js

function Map() {
    
    const {
        initializeMap,
    } = useKakaoMap();
    
    useEffect(() => {
        initializeMap();
    }, [initializeMap]);
    
    
    return(
     <div id="map"></div>
    
    )
    


Generally, necessary functions for the map, such as drag events, click events, and marker handling,

are better implemented in custom hooks rather than within the component,

in terms of reusability and readability.


Reason

You need to understand the concept of React to know why React doesn't access global objects.


1) Encapsulation and Modularity

- Each component should be independent and reusable. Depending on global objects in numerous components will break independence and create complexity.


2) Performance and Easier Maintenance

- React re-renders when the state changes, and global objects are not suitable for tracking state changes.


3) State Management is Centralized

- If you still need to share multiple variables, use a central state management library like Redux.

Center -> Control data flow to components to prevent errors and make maintenance easier.


* Server-side rendering

Recently, server-side rendering is widely used for SEO (Search Engine Optimization), but the window object cannot be accessed from the server-side!!


Thoughts

This was a trial and error I encountered during my first React project.

I've been doing real-world projects with Vue for over 2 years, but when I started a React project,

I was quite surprised that I couldn't display Kakao Map...!!!


Compared to Vue, it feels a bit more free in terms of folder structure and hooks, making it more difficult.

I am thinking a lot about what is the most efficient way to write it.

I need to practice more with this toy project, literally treating it as a toy.


* This is my first blog, and durumis is quite unique and fun.

It also provides a 3-line summary that Koreans love, haha.

Could I meet developers from overseas?

I wish the code editor could be improved a little.


I welcome any feedback or criticism!

Have a happy day!




๋š ๋š ๋ฉ
๋š ๋š ๋ฉ์˜ ์ƒ๊ฐ๋“ค
๋š ๋š ๋ฉ์˜ ์ƒ๊ฐ๋“ค
๋š ๋š ๋ฉ
Basic CSS Rules (Normal flow, BFC, IFC) This article explains the fundamental concepts of CSS layout: block elements, inline elements, margin, alignment, and Block Formatting Context (BFC) and Inline Formatting Context (IFC). It delves into the normal flow of CSS elements, the characteristics o

September 7, 2024

[React Hook] useState This article explains how React Hook useState works, its re-rendering scope, and how to maintain state values, delving into its internal implementation.
Sunrabbit
Sunrabbit
Sunrabbit
Sunrabbit

March 14, 2024

FAQ Collection durumis is a multilingual, free writing service. It is currently in beta and lacks some features, but we are constantly updating it. Please check out our frequently asked questions and answers.
durumis official blog
durumis official blog
Image that says FAQ
durumis official blog
durumis official blog

January 24, 2024

An Alien Application Developer's First Story This is the story of a developer who started a global blog by choosing durumis. The goal is to promote an international dating application using 38 language support and automated YouTube translation tools. He is also the operator of an international coupl
Alien Story
Alien Story
This is the story of a developer who started a global blog by choosing durumis. The goal is to promote an international dating application using 38 language support and automated YouTube translation tools. He is also the operator of an international coupl
Alien Story
Alien Story

April 21, 2024

A record of the development trials and tribulations of Korea Investment & Securities API This blog post is for developers who want to create an automated trading program using Korea Investment & Securities API. It introduces the difficulties encountered in the development process, such as account opening, unsupported mock trading, and Websock
(๋กœ๋˜ ์‚ฌ๋Š” ์•„๋น ) ์‚ด๋ฆผ ํ•˜๋Š” ์—„๋งˆ
(๋กœ๋˜ ์‚ฌ๋Š” ์•„๋น ) ์‚ด๋ฆผ ํ•˜๋Š” ์—„๋งˆ
This blog post is for developers who want to create an automated trading program using Korea Investment &amp; Securities API. It introduces the difficulties encountered in the development process, such as account opening, unsupported mock trading, and Websock
(๋กœ๋˜ ์‚ฌ๋Š” ์•„๋น ) ์‚ด๋ฆผ ํ•˜๋Š” ์—„๋งˆ
(๋กœ๋˜ ์‚ฌ๋Š” ์•„๋น ) ์‚ด๋ฆผ ํ•˜๋Š” ์—„๋งˆ

April 23, 2024

Difficulties Encountered While Developing the Korea Investment & Securities API This blog post delves into the challenges and solutions encountered during the development process of the Korea Investment & Securities API. It shares a developer's experiences and tips, covering topics like account opening, ISA account transfer, unsuppor
(๋กœ๋˜ ์‚ฌ๋Š” ์•„๋น ) ์‚ด๋ฆผ ํ•˜๋Š” ์—„๋งˆ
(๋กœ๋˜ ์‚ฌ๋Š” ์•„๋น ) ์‚ด๋ฆผ ํ•˜๋Š” ์—„๋งˆ
This blog post delves into the challenges and solutions encountered during the development process of the Korea Investment &amp; Securities API. It shares a developer's experiences and tips, covering topics like account opening, ISA account transfer, unsuppor
(๋กœ๋˜ ์‚ฌ๋Š” ์•„๋น ) ์‚ด๋ฆผ ํ•˜๋Š” ์—„๋งˆ
(๋กœ๋˜ ์‚ฌ๋Š” ์•„๋น ) ์‚ด๋ฆผ ํ•˜๋Š” ์—„๋งˆ

April 23, 2024

Korea Investment & Securities API Development Reference This blog post describes the process of collecting information needed to develop an automated trading program using the Korea Investment & Securities API. You can get the information needed for automated trading program development using various material
(๋กœ๋˜ ์‚ฌ๋Š” ์•„๋น ) ์‚ด๋ฆผ ํ•˜๋Š” ์—„๋งˆ
(๋กœ๋˜ ์‚ฌ๋Š” ์•„๋น ) ์‚ด๋ฆผ ํ•˜๋Š” ์—„๋งˆ
This blog post describes the process of collecting information needed to develop an automated trading program using the Korea Investment &amp; Securities API.  You can get the information needed for automated trading program development using various material
(๋กœ๋˜ ์‚ฌ๋Š” ์•„๋น ) ์‚ด๋ฆผ ํ•˜๋Š” ์—„๋งˆ
(๋กœ๋˜ ์‚ฌ๋Š” ์•„๋น ) ์‚ด๋ฆผ ํ•˜๋Š” ์—„๋งˆ

April 22, 2024