标签: create-react-app

在 Heroku 上配置单页面 Create-React-App

使用 Firebase 托管,当您部署应用程序时,系统会询问您是否将所有网址重写为 /index.html。这意味着无论是直接访问还是从主页导航到,react-router路由都会被正确触发。

我们使用Create-react-app和Create-react-app-buildpack部署到 Heroku。我们怎样才能以相同的方式将所有 URL 重写为 /index.html 呢?

在这个问题之后,解释了routes指令,我在根目录中配置了一个static.json,但没有效果:

{
  "routes": {
    "/images/*": "/images/",
    "/legal/*": "/images/",
    "/media/*": "/media/",
    "/fav/*": "/fav/",
    "/styles/*": "/styles/",
    "/**": "index.html"
  }
}
Run Code Online (Sandbox Code Playgroud)

heroku buildpack reactjs create-react-app

4
推荐指数
1
解决办法
828
查看次数

无法在 npm run build 上缩小代码

我已经完成了使用创建的反应应用程序 create-react-app ,在本地运行时没有任何问题npm start,但是当我尝试使用构建时 npm-build

我在缩小代码时遇到错误。我尝试克隆我的项目并尝试构建它,我尝试创建一个测试项目并尝试npm build并且没有任何问题,只有当我尝试构建我的完整项目时。

这是我的错误

> test@0.1.0 build C:\Users\Jonathan Stroz\Documents\HackerNews-Production-master
> react-scripts build

Creating an optimized production build...
Failed to compile.

Failed to minify the code from this file:

        ./node_modules/newsapi/index.js:20

Read more here: 

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! test@0.1.0 build: `react-scripts build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the test@0.1.0 build script.
npm ERR! This is probably not a problem with npm. There …
Run Code Online (Sandbox Code Playgroud)

javascript json npm reactjs create-react-app

4
推荐指数
1
解决办法
8435
查看次数

如何将 .env.qa 或 .env.staging 与 create react app 一起使用

CreateReactApp 文档显示您可以使用有限数量的 .env 文件。 https://create-react-app.dev/docs/adding-custom-environment-variables/#adding-development-environment-variables-in-env

我想在其他环境中使用更多 .env 文件,但.env.development总是被使用。

如何使用其他文件?我找到了这篇文章,但它对我不起作用。 https://medium.com/@tacomanator/environments-with-create-react-app-7b645312c09d

我正在使用react-scriptsv3.4.1

reactjs create-react-app dotenv env-cmd

4
推荐指数
1
解决办法
4779
查看次数

打字稿 d.ts 文件中的枚举

我是 Typescript 的新手,我在 create-react-project 中使用它。我有一个名为 /typings 的文件夹,我的 tsconfig.json 文件指向它,到目前为止,我已将所有类型声明放在该文件夹中的index.d.ts文件中。

到现在为止还挺好。“类型”和“接口”声明似乎在我的项目的所有部分都可用。我没有明确地从index.d.ts文件中导出它们,也没有将它们导入到任何其他文件中。

当我声明以下内容时,问题就出现了......

enum Gender {male, female}
Run Code Online (Sandbox Code Playgroud)

当我尝试在不同的文件中使用枚举时,出现错误...

Ambient const enums are not allowed when the '--isolatedModules' flag is provided
Run Code Online (Sandbox Code Playgroud)

我已经阅读了关于 SO 的其他答案,其中说您应该将枚举声明为常量,但这无济于事。我还看到建议您应该在文件底部“导出默认未定义”,但是当我这样做时,项目的其余部分中没有其他类型可用。

最后,我尝试更改我的编译器选项以设置"isolatedModules": false但在编译时它会自动切换回 true。显然 create-react-app 总是会这样做。

我应该怎么做才能让我的 index.d.ts 文件中声明的枚举可以自动用于我的项目的其余部分?

enums typescript create-react-app

4
推荐指数
1
解决办法
1527
查看次数

如何在我的 React 应用程序中修复 @fontface 问题?

我正面临自定义字体的问题。我使用 create-react-app 创建了一个应用程序,并在公共文件夹中提供了自定义字体文件 (.ttf),以便在构建应用程序时,所有资产都是构建的一部分。我可以在本地看到字体,但在 nginx 服务器上看不到。Nginx 服务器有 .ttf 支持,因为另一个应用程序工作正常。失恋应该是什么?我无法找到它。此外,firefox 无法加载相同的自定义字体。这是我的样式表 -

@font-face {
    font-family: 'Simplied';
    src: url('/fonts/simplied-Light.ttf') format('truetype');
  }
Run Code Online (Sandbox Code Playgroud)

我使用@import 'stylesheet.css' 将其导入到另一个 css 文件中。

PS感谢您的评论。我做了这样的改变 -

//  ./index.css
@font-face {
  font-family: 'Simplified_Lt';
  font-display: block;
  src: local('Simplified_Lt') url(./fonts/Simplified_Lt.ttf) format('truetype');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC,
    U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

body {
  margin: 0;
  font-family:'Simplified_Lt';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
Run Code Online (Sandbox Code Playgroud)

我的字体文件夹在 src/fonts/ 下。但是我仍然无法使用该字体。请您指出可能遗漏了什么或项目结构是否正确?我正在使用 create-react-app。

css font-face reactjs create-react-app

4
推荐指数
1
解决办法
3369
查看次数

如何让 jest 使用与 create-react-app 上的 react-script 测试相同的配置运行?

我知道我可以运行npm testwhichreact-script test并且它可以工作,它成功地运行了测试。但我有兴趣弄清楚如何使用jest相同的配置直接运行react-script。希望不必复制配置或弹出应用程序。我开始阅读的源代码,react-scripts但到目前为止我无法弄清楚。

想要这个的原因是:

  • 我的 CRA 项目是一个更大项目的一部分,我可以jest在顶层运行并运行所有测试。
  • 在 WebStorm 中,我可以利用 Jest 集成,其中包括:
    • 显示运行时通过或失败的测试列表。
    • 能够运行单独的测试。
    • 做代码覆盖。

如果我jest在我的 CRA 应用程序上运行,我会收到此错误:

PS C:\Users\pupeno\Documents\Flexpoint Tech\js\exp7\frontend> jest
 FAIL  src/App.test.tsx
  ? Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use …
Run Code Online (Sandbox Code Playgroud)

reactjs jestjs create-react-app

4
推荐指数
1
解决办法
2195
查看次数

如何在 useEffect 钩子中正确实现 props 回调函数

我想用来useEffect()检测状态值的变化,并使用作为道具接收的回调函数将该值传递给父组件。如果不禁用 eslint 缺少依赖项警告,我无法找到一种方法。我在子组件和那个孩子的孩子上都有这个问题。

这是父实现:

const updateValues = (newValues) => {
  setValues({ ...values, ...newValues });
};

<GeneralUpdates onUpdate={updateValues} />
Run Code Online (Sandbox Code Playgroud)

这是第一个孩子(GeneralUpdates):

const [values, setValues] = useState({
    name: '',
    description: '',
  });

// This handles form input changes
const handleChange = (prop) => (event) => {
  setValues({ ...values, [prop]: event.target.value });
};

useEffect(() => {
  onUpdate(values);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [values]);

<FilesUpload handleChange={onUpdate}/>

Run Code Online (Sandbox Code Playgroud)

这是孩子的孩子(文件上传):

const [featuredPhotos, setFeaturedPhotos] = useState([]);

useEffect(() => {
  handleChange({ featuredPhotos });
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [featuredPhotos]);
Run Code Online (Sandbox Code Playgroud)

添加 …

reactjs eslint create-react-app react-hooks

4
推荐指数
2
解决办法
1190
查看次数

使用 create-react-app 创建新的 React 应用程序时遇到问题

我正在尝试创建一个新的反应项目,但是当我运行时npx create-react-app tik-tok-clone出现以下错误

    Creating a new React app in C:\Users\mwars\Documents\GitHub\TikTok-Clone\tik-tok-clone.

Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts with cra-template...

yarn add v1.22.4
[1/4] Resolving packages...
[2/4] Fetching packages...
error postcss@8.1.3: The engine "node" is incompatible with this module. Expected version "^10 || ^12 || >=14". Got "13.12.0"
error Found incompatible module.
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.

Aborting installation.
  yarnpkg add --exact react react-dom react-scripts cra-template --cwd C:\Users\mwars\Documents\GitHub\TikTok-Clone\tik-tok-clone has …
Run Code Online (Sandbox Code Playgroud)

npm reactjs create-react-app npx

4
推荐指数
1
解决办法
1060
查看次数

使用测试库 (React) 找不到按钮

我一定遗漏了一些很明显的东西,我有一个反应应用程序,它有一个带有多个按钮的左 div,每个按钮都有不同的文本。我想检查这些按钮中的每一个是否都已呈现。例如,为了找到其中一个按钮(使用测试库),我正在执行以下操作:

screen.getByText("/channels/i", { selector: "button" })
Run Code Online (Sandbox Code Playgroud)

我也试过

screen.getByRole("button", { name: /channels/i })
Run Code Online (Sandbox Code Playgroud)

但我总是得到结果

TestingLibraryElementError: Unable to find an accessible element with the role "button" and name `/channels/i`

    There are no accessible roles. But there might be some inaccessible roles. If you wish to access them, then set the `hidden` option to `true`. Learn more about this here: https://testing-library.com/docs/dom-testing-library/api-queries#byrole
Run Code Online (Sandbox Code Playgroud)

但正如你所看到的,它确实存在

在此处输入图片说明

像那样,我可能还有另外 10 个按钮,所以我不明白为什么它说没有可访问的角色。

我做错了什么?

reactjs create-react-app react-testing-library

4
推荐指数
2
解决办法
4794
查看次数

新的 React 应用程序在安装后无法立即编译

我已经使用 React Native 有一段时间了,但我想我会在网络上试用 React。所以我遵循了这个指南:https : //reactjs.org/docs/create-a-new-react-app.html但在使用后npx create-react-app react-try,导航到新文件夹,然后输入yarn start我收到此错误消息:

Failed to compile.

./src/index.js 1:60
Module parse failed: Unexpected token (1:60)
File was processed with these loaders:
 * ./node_modules/@pmmmwh/react-refresh-webpack-plugin/loader/index.js
 * ./node_modules/react-scripts/node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
> $RefreshRuntime$ = require('C:/Users/e096752/Documents/Cole's Git Repos/react-try/node_modules/react-refresh/runtime.js');
| $RefreshSetup$(module.id);
Run Code Online (Sandbox Code Playgroud)

这是包含在 create 方法中的所有内容: Project

包.json

{
  "name": "react-try",
  "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",
    "react": "^17.0.1", …
Run Code Online (Sandbox Code Playgroud)

npm reactjs create-react-app yarnpkg

4
推荐指数
1
解决办法
3889
查看次数