Camilla
young Camilla
Camilla
  • 전체보기 (90)
    • Data Analysis (1)
    • SAP (5)
      • SAP Datasphere (0)
      • SAP HANA DB (1)
      • SAP Analytics Cloud (0)
      • SAP BW (4)
    • Web (51)
      • JavaScript (8)
      • React (10)
      • WebRTC (3)
      • node.js (7)
      • Vue (2)
      • CSS (2)
      • 기타 (19)
    • CS (13)
      • Network (8)
      • OS (5)
    • 기타 (2)
      • Git (1)
      • Unity (1)
    • 알고리즘 문제 풀이 (11)
      • 백준 (9)
      • 프로그래머스 (2)
    • 회고 (6)
    • 취준 (0)

More

  • 방명록
  • Github

태그

  • fontawsomereact
  • 리액트프로젝트
  • 리액트
  • JavaScript
  • 리액트채팅
  • 채팅기능구현
  • fontawsome
  • 리액트아이콘
  • fontawsome리액트

최근 댓글

인기 글

티스토리

hELLO · Designed By 정상우.
Camilla

young Camilla

[React] componentDidMount 에서 async/await 사용하기
Web/React

[React] componentDidMount 에서 async/await 사용하기

2022. 8. 4. 23:25

클래스 컴포넌트 생명주기 함수 중 하나인 componentDidMount() 안에서 await을 사용하는데 다음과 같은 에러가 났다.

Parsing error: Unexpected reserved word 'await'.

  componentDidMount() {
    await tmModel.loadModel() // teachable machine 로드
  }

해결 방법은 componentDidMount 앞에 async를 붙여주면 된다. (Create-react-app으로 프로젝트를 만든 경우)

 async componentDidMount() {
    await tmModel.loadModel() // teachable machine 로드
  }

CRA로 만든 프로젝트가 아닌 경우 웹팩 설정을 수정해줘야 한다.

우선 babel preset env, babel plugin transform runtime을 설치 해주고 .babelrc 설정 파일을 수정한다.

npm i @babel/preset-env @babel/plugin-transform-runtime @babel/runtime --save-dev
{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "browsers": [
            ">0.25%",
            "not ie 11",
            "not op_mini all"
          ]
        }
      }
    ],
    "@babel/preset-react"
  ],
  "plugins": [
    "@babel/plugin-transform-runtime"
  ]
}

출처 : https://www.valentinog.com/blog/await-react/

저작자표시 비영리 변경금지 (새창열림)
    'Web/React' 카테고리의 다른 글
    • [React] url query string 파싱하기- 리액트 라우터,타입스크립트
    • [React] Props Getters 패턴
    • [React] Props collection 패턴으로 prop 전달하기
    • [React] custom hook 활용
    Camilla
    Camilla
    BI Engineer Data Warehouse & Visualization

    티스토리툴바