我有一个我已经设置的 create-react-app 项目(express 和 node 连接到后端的 mysql 数据库,以防万一),并且它工作得很好。我的项目经理告诉我,我需要将“public”目录的名称更改为“static”(该目录保存/将保存我的静态文件,例如我的 index.html 和我需要提供的任何媒体)为了使其正确部署,但是我能找到的唯一解决方案是弹出,以便我可以更改“PUBLIC_URL”的值。目前,当我运行“npm run start”时出现错误,其中记录:“找不到所需的文件。名称:index.html',它仍在公共目录中搜索(我已将其更改为“静态”)。我的代码中没有任何地方引用公共文件夹。
根据我的理解(如果我错了,请有人纠正我), PUBLIC_URL 变量由 create-react-app 设置为公共文件夹的路径。我想将其更改为静态文件夹,但我发现的唯一解决方案似乎可能涉及弹出然后手动更改变量。有没有办法在不弹出的情况下设置该变量,或者做一些其他的解决方法来更改该目录,如果没有,最简单的方法是什么(我宁愿不弹出,但如果它是唯一的方法)...我真的只需要目录来更改名称,并且我发现自己对我认为相当简单的更改感到沮丧。
谢谢!
编辑:这是我的 package.json:
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.5.2",
"react-dom": "^16.5.2",
"react-scripts": "1.1.5"
},
"scripts": {
"start": "PORT=8080 && react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
Run Code Online (Sandbox Code Playgroud) 由于无法编辑 webpack 配置文件,因此解决渲染阻塞 CSS 以及仅使用 Create React App 加载关键 CSS 的最佳方法是什么?
reactjs webpack create-react-app render-blocking critical-css
"exclude": ["*.test.ts", "*.test.tsx"]tsconfig 中仅阻止测试特定类型的类型检查(例如,describe、it、expect 等)。我仍然在 vscode 的每个测试文件中看到导入组件的错误。禁用这些导入的组件错误的唯一方法是//@ts-nocheck在每个测试文件的开头添加。
有没有办法禁用 tsconfig 中所有测试文件的所有类型检查,包括来自非测试文件的任何导入组件?
完整tsconfig.json文件(创建 React App 项目)
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"noImplicitAny": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": ["src"],
"exclude": ["*.test.ts", "*.test.tsx"]
}
Run Code Online (Sandbox Code Playgroud) 我按照此文档将 CSS 重置添加到我的应用程序中。
https://create-react-app.dev/docs/adding-css-reset/#indexcss
但它显示了这条消息:
"stylelint": {
"extends": "stylelint-config-recommended",
"rules": {
"at-rule-no-unknown": null
}
Run Code Online (Sandbox Code Playgroud)
怎么解决这个问题啊,好烦啊……
在yarn v1中,我们使用.createreactapp初始化yarn create react-app my-app。如何在 yan v2 中初始化相同的内容。
上面的命令给出了错误
Usage Error: No project found in <<directory>>
$ yarn run <scriptName> ...
Run Code Online (Sandbox Code Playgroud) 我有一个 create-react-app 使用File System Access API读取和写入本地文件。当在浏览器(支持它的 Chrome 或 Edge)中运行时,读取和写入文件都可以正常工作。
当应用程序在 Electron 中运行时,读取可以工作,但写入会失败,原因是:Uncaught (in promise) DOMException: The request is not allowed by the user agent or the platform in the current context.
我使用的是最新的 Electron (12.0.1),它使用与我的 Chrome 浏览器相同的 Chromium (89.0.4389.82)。
下面是相关代码。调用后的控制台日志在浏览器和Electron中requestPermission显示true和。grantedtruedenied
我尝试webSecurity在创建时禁用BrowserWindow,禁用沙箱,appendSwitch但没有任何帮助。
有没有办法给Electron中的Chromium更多的权限?
如果没有,我愿意在 Electron 中以不同的方式处理文件写入。那么,TODO在代码中应该写什么来代替呢?请注意,因为它是一个 create-react-app,所以该fs模块不可用。
export async function chooseAndReadFile() {
const fileHandle = await window.showOpenFilePicker().then((handles) => handles[0])
const file …Run Code Online (Sandbox Code Playgroud) file reactjs electron create-react-app file-system-access-api
我正在使用 create-react-app 并为 eslint 配置了我的项目。下面是我的 .eslintrc 文件。
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint",
"react-hooks"
],
"extends": [
"plugin:prettier/recommended",
"airbnb-typescript-prettier",
"prettier",
"plugin:import/typescript"
],
"parserOptions": {
"project": "./tsconfig.json"
},
"rules": {
// Make prettier code formatting suggestions more verbose.
"prettier/prettier": [
"warn"
],
"camelcase": "warn",
"no-console": "error",
"no-prototype-builtins": "warn",
"arrow-body-style": "warn",
"prefer-arrow-callback": "warn",
"jsx-a11y/click-events-have-key-events": "warn",
"jsx-a11y/no-noninteractive-element-interactions": "warn",
// Disable <Fragment> => <> replacement. Feel free to change
"react/jsx-fragments": "off",
"react/destructuring-assignment": "warn",
"react/no-unused-prop-types": "warn",
//TODO: Remove this rule later
"react/jsx-props-no-spreading": "off", …Run Code Online (Sandbox Code Playgroud) 我正在使用react-router-dom和create-react-app。
运行脚本yarn start,它以http://localhost:3000/(myprojectname),而不是开头http://localhost:3000/
当使用react-router-dom进行路由时,我必须myprojectname从url中删除,然后添加页面路由。
项目的初始设置好像有问题,
我该如何开始http://localhost:3000/?
添加package.json、路由器代码。
包.json:
{
"name": "cau-burger-online-order-system",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@types/jest": "^26.0.15",
"@types/node": "^12.0.0",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@types/react-router-dom": "^5.3.2",
"@types/styled-components": "^5.1.16",
"axios": "^0.24.0",
"bootstrap": "^5.1.3",
"brace-expansion": "^2.0.1",
"gh-pages": "^3.2.3",
"prettier": "^2.5.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "5.3.0",
"react-scripts": "4.0.3",
"styled-components": "^5.3.3",
"typescript": "^4.1.2",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": …Run Code Online (Sandbox Code Playgroud) 我有一个带有 ReactJS 的 ASP.NET 6 应用程序,是前段时间使用 Visual Studio 2022ASP.NET Core with React.js模板创建的。
ClientApp 是一个使用create-react-app.
我已将我的react-scripts包更新到版本 5.0.0(从 4.0.3)。AFAIK 的重大变化之一是它现在使用 webpack 5。
自此更新以来,当我启动 ASP.NET 应用程序(使用同时启动 ASP.NET 应用程序和 React 应用程序的标准运行配置)时,只要我在任何 React 文件中进行更改,热重新加载就不会自动刷新浏览器。如果我手动点击浏览器的刷新按钮或 F5,我可以看到更改。唯一的变化是,在 React 文件发生更改后,浏览器不会自行刷新。
我使用的是 Windows 11。
这是我目前的package.json:
{
"name": "my.app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@types/jest": "^27.0.3",
"@types/node": "^16.11.9",
"@types/react": "^17.0.35",
"@types/react-dom": "^17.0.11",
"@types/react-router-bootstrap": "^0.24.5",
"bootstrap": "^5.1.3",
"jquery": "^3.5.1",
"merge": "^1.2.1",
"node-sass-chokidar": "^1.5.0",
"npm-run-all": "^4.1.5",
"oidc-client": "^1.9.0",
"react": "^16.0.0",
"react-bootstrap": …Run Code Online (Sandbox Code Playgroud) 我使用创建了一个新的反应项目npx create-react-app app-name --template typescript,它运行良好,直到我导入另一个.tsx文件。现在,当我尝试导入另一个.tsx文件时,出现错误:Module not found: Error: Can't resolve './components/component' in '/Users/benbaldwin/Desktop/project/frontend/teacher/src'
我做的唯一与平常不同的是文件结构。由于我的文件夹中有多个frontend共享依赖项的反应项目,因此我将 移动到node_modules覆盖所有这些子目录。我的文件结构如下所示:
- frontend
- node_modules
- project-1
- public
- src
index.tsx
react-app-env.d.ts
- components
component.tsx
- project-2
package.json
tsconfig.json
Run Code Online (Sandbox Code Playgroud)
组件文件如下所示:
- frontend
- node_modules
- project-1
- public
- src
index.tsx
react-app-env.d.ts
- components
component.tsx
- project-2
package.json
tsconfig.json
Run Code Online (Sandbox Code Playgroud)
索引文件如下所示:
import React from 'react';
const Component: React.FC = () => {
return <div>hello</div>;
}; …Run Code Online (Sandbox Code Playgroud) create-react-app ×10
reactjs ×8
javascript ×3
webpack ×3
typescript ×2
asp.net-core ×1
critical-css ×1
electron ×1
eslint ×1
file ×1
npm ×1
stylelint ×1
yarn-v2 ×1
yarnpkg ×1