在 Google Chrome 上加载我的网站时,我收到了一些类似于以下内容的警告:
DevTools failed to load SourceMap: Could not parse content for https://mywebsite.com/static/js/2.abcd1234.chunk.js.map: Unexpected token < in JSON at position 0
Run Code Online (Sandbox Code Playgroud)
我的 web 应用程序是部署在 AWS Amplify 上的 React 应用程序 (create-react-app)。在本地运行应用程序时,我没有收到这些警告。我如何摆脱这些警告?
我有一个 Create React App 项目,目前正在从 FlowJS 迁移到 TypeScript。当我将 TypeScript 集成到项目中时,WebStorm 中出现的一个问题是 TypeScript 似乎无法解析 SCSS 模块导入中的任何键。我总是受到以下欢迎:
我的顶级样式导入是:import styles from './_styles.module.scss'
经过大量研究/深入尝试解决此问题后,我创建了一个src/global.d.ts包含以下内容的文件:
declare module '*.scss' {
const content: {
[className: string]: string;
}
export = content
}
Run Code Online (Sandbox Code Playgroud)
尽管定义了上述模块、使缓存失效并重新启动 WebStorm,问题仍然存在——我不再知道还可以尝试什么来解决这个问题。
我的 TypeScript 版本设置为"typescript": "^3.8.3"我的package.json. 这就是我的tsconfig.json样子:
{
"compilerOptions": {
"allowJs": false,
"allowSyntheticDefaultImports": true,
"baseUrl": "src",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"jsx": "react",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"module": "esnext",
"moduleResolution": "node",
"noEmit": true, …Run Code Online (Sandbox Code Playgroud) 目前,$ npm run build将文件编译为src/,/build一切正常。但是,有一些特定的 TypeScript 文件,src/integer.ts我想将其编译为其自己的 JavaScript 文件,build/integer.js并且所有其他文件都将“正常”编译。
这是项目结构:
/src
/build
Run Code Online (Sandbox Code Playgroud)
这是package.json文件:
"dependencies": {
"react-scripts": "3.4.1",
}
...
"scripts": {
"build": "react-scripts build",
}
Run Code Online (Sandbox Code Playgroud) 我有一个使用 Jest 和 React-testing-library 以及 .tsx 测试文件(TypeScript)的测试:
import React from "react";
import { Provider } from 'react-redux';
import { render, screen } from "@testing-library/react";
import Home from "./Home";
describe('Home', () => {
test("renders Programs without crashing", async () => {
const storeFake = (state: any) => ({
default: () => { },
subscribe: () => { },
dispatch: () => { },
getState: () => ({ ...state })
});
const store = storeFake({}) as any;
const { getByAltText } = …Run Code Online (Sandbox Code Playgroud) typescript reactjs jestjs create-react-app react-testing-library
您好,我有一个静态 html 登陆页面,它有自己的 css、静态资源,并链接到其他几个静态页面。所以整个迷你项目都有自己的index.html、about-us.html、contact.html等。此外,我还通过CRA构建了一个React应用程序,其中我有一个非常酷的仪表板,现在我想将两者结合起来我的网站的入口点是登陆页面,然后当您转到 /dashboard 路径时,这是我的反应应用程序我怎样才能实现这一点?
因此,我的静态登陆页面项目的项目结构如下所示:
/homepage
--index.html
--contact.html
--about-us.html
--/static
----css files, imgs, ect
Run Code Online (Sandbox Code Playgroud)
我的 React 项目使用 React 路由器,因此我在路径 /dashboard 处有一个功能齐全的 React 项目
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import * as serviceWorker from "./serviceWorker";
import { Route, BrowserRouter as Router, Switch } from "react-router-dom";
import Login from "./components/Login/Login";
import Dashboard from "./components/Dashboard";
import SignupPage from "./components/Signup/SignupPage";
const App = (
<Router>
<Switch>
<Route exact path="/login" component={Login} />
<Route exact path="/signup" component={SignupPage} />
<PrivateRoute …Run Code Online (Sandbox Code Playgroud) 我想测试React并发模式,并且仍然使用create-react-app?是否可以?
我检查了create-react-app文档,也查找了指定的NPM模块,但没有找到。
我有两个存储库fe和craft,分别是应用程序库和组件库。
两个存储库都可以自行编译。但是,当我尝试通过组件库使用组件时,如果组件本身props.theme在styled-components.
Main.tsx来自fe
import React from "react";
import { Button, HeadlineOne } from "handcraft";
const Page = (props: any) => {
console.log(props.theme);
return (
<>
<Button onClick={() => {}}>Sample</Button> // This causes TypeError: Cannot read property 'primary' of undefined.
<HeadlineOne>Sample</HeadlineOne> // This is fine.
</>
);
};
export default Page.
Run Code Online (Sandbox Code Playgroud)
来自Button.tsxcraft(不行,访问 props)。
import React, { ReactNode } from "react";
import { Button as BaseButton } from "@material-ui/core"; …Run Code Online (Sandbox Code Playgroud) 我jsconfig.json在我的 vscode 项目中添加了一个,由于某种原因,我收到一个打字稿错误,指出“dotenv/types未找到”。
我一生都无法弄清楚为什么会出现这个错误。我什至尝试将 dotenv 添加到我的中package.json,但它没有解决我的问题。
这是使用 create-react-app 创建的标准项目,我添加了jsconfig.json来允许绝对导入
{
"compilerOptions": {
"baseUrl": "src"
},
"include": ["src"]
}
Run Code Online (Sandbox Code Playgroud)
例外:
找不到文件“/Users/path/to/file/node_modules/dotenv/types”。
其他可能有用的潜在文件:
package.json
{
"name": "quantous-spa",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"axios": "^0.20.0",
"babel-plugin-macros": "^2.8.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-feather": "^2.0.8",
"react-loading-skeleton": "^2.1.1",
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.3",
"styled-components": "^5.1.1",
"typeface-muli": "^1.1.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject" …Run Code Online (Sandbox Code Playgroud) javascript typescript reactjs visual-studio-code create-react-app
如何使用 React App 连接多个 API?
使用 http-proxy-middleware 与 Express 后端创建-react-app。
演示错误: https: //github.com/svgpubs/nodeproxy.git
我正在尝试http-proxy-middleware将演示 React 应用程序连接到两个不同的服务器:一个外部网站https://api.coingecko.com/api/和一个内部网站http://localhost:3001/
它适用于外部网站。但是,localhost:3001 连接不起作用。
如果我不使用 http-proxy-middleware (通过添加'proxy: 'http://localhost:3001'"),我可以连接到 localhost:3001 package.json- 但是,那么我只能有一个代理。
这是正在运行的应用程序:如您所见,localhost:3001 没有响应
错误:我尝试了很多不同的变体。我要么从浏览器获取 cors 块,要么 localhost api 从 public/index.html 返回 index.html 文件 - 导致浏览器中出现 json 解析错误。在服务器上,根据 localhost 端点的确切路由,我有时会收到 50 多行此错误:
Error occurred while trying to proxy request /localhostapi/users from localhost:3001 to http://localhost:3001/ (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)
如何设置服务器和代理,以便 App.js 可以连接到 localhost:3001 路由和外部 API?
这是我的App.js
import React, { useEffect, …Run Code Online (Sandbox Code Playgroud) proxy express create-react-app http-proxy-middleware react-fullstack
我已按照本指南扩展我的 ESLint 配置。
https://create-react-app.dev/docs/setting-up-your-editor/#experimental-extending-the-eslint-config
该文档指出Note that any rules set to "error" will stop the project from building。我已在与我的文件相同的文件夹中添加了一个.env文件。EXTEND_ESLINT=truepackage.json
https://create-react-app.dev/docs/advanced-configuration/
添加此文件后,npm run build即使使用 package.json 中的默认值,我也不再收到来自命令的警告:
"eslintConfig": {
"extends": "react-app"
},
Run Code Online (Sandbox Code Playgroud)
如果我将.env文件设置为EXTEND_ESLINT=false警告,警告就会再次出现。
首先这样做的原因是因为我想添加no-multiple-empty-lines这样的规则:
"eslintConfig": {
"extends": [ "react-app" ],
"rules": {
"no-multiple-empty-lines": [
"error",
{
"max": 1,
"maxEOF": 0,
"maxBOF": 0
}
]
}
},
Run Code Online (Sandbox Code Playgroud)
https://eslint.org/docs/rules/no-multiple-empty-lines
我认为配置是正确的,因为当我添加此规则时,以下命令将显示预期结果:
npx eslint . --ext .js,.jsx,.ts,.tsx
Run Code Online (Sandbox Code Playgroud)
配置更改前:
配置更改后:
读取.env文件时,它表示npm run …
create-react-app ×10
reactjs ×7
typescript ×5
javascript ×4
aws-amplify ×1
eslint ×1
express ×1
frontend ×1
html ×1
jestjs ×1
proxy ×1
react-router ×1
webpack ×1
webstorm ×1