1、執行 npx create-react-app my-app --typescript 無效
參考 Create React App 官方網站,使用模板初始化React項目的最新指令如下:
npx create-react-app my-app --template [template-name]
附官網地址:https://create-react-app.dev/docs/getting-started/#selecting-a-template
重新運行以下指令安裝
- npm uninstall -g create-react-app // 或 - yarn global remove create-react-app npx create-react-app my-app --template typescript
2、VsCode 提示 “Cannot use JSX unless the '--jsx' flag is provided”(無法使用 JSX,除非提供了 "--jsx" 標志。)
正確的解決步驟:
(1)、打開Vscode,通過 Ctrl + Shift + P 打開工作區設置
(2)、選擇Typescript 版本
(3)、拋棄Vscode使用的4.0.3版本,選擇較新的4.1.2版本
3、啟動項目后,控制台報錯 Uncaught TypeError: Cannot read property 'forEach' of undefined at Object.injectIntoGlobalHook (react-refresh-runtime.development.js:465)
解決辦法:禁用瀏覽器擴展程序中的 React 開發者工具
重新啟動項目,可以正常打開頁面了