标签: create-react-app

axios 403 禁止 OPTIONS 请求

我正在开发一个使用 React+Redux 并用于axios进行 api 调用的应用程序。

这是一个失败的调用示例:

axios.post(`${API_ROOT}${FIND_USER}${currentUserID}`, {
        headers: {
                'Authorization': token
        },
      })
    .then((response) => {
        console.log("Sucess")
    })
Run Code Online (Sandbox Code Playgroud)

当我看到的请求网址network是这样的:

http://domainName:8080/users/findUser/1234
Run Code Online (Sandbox Code Playgroud)

API 调用OPTIONS本身失败,我从后端收到的错误是

Response for preflight has invalid HTTP status code 403
Run Code Online (Sandbox Code Playgroud)

它永远不会达到POST。

token是从 检索到的localstorage,类似于Bearer eyJhbGci...。

后端开发人员正在使用 Java 和 Spring 。

我调用 API 的方式是否有问题,或者这个问题应该在后端解决?

通过 Postman 测试时,这个 API 工作得很好。

java reactjs redux axios create-react-app

6
推荐指数
1
解决办法
9779
查看次数

如何在创建 React 应用程序中运行 eslint

帮助在创建 React 应用程序中运行 eslint。

我用教程创建了 React 应用程序

然后我安装了

babel-eslint

standard

snazzy
Run Code Online (Sandbox Code Playgroud)

然后我将下一行添加到 package.json 中的脚本中

"lint": "standart --verbose | snazzy"
Run Code Online (Sandbox Code Playgroud)

现在我尝试使用命令运行 eslint:npm run lint

但我有一个错误Parsing error: Unexpected token = null

我的完整 package.json 如下

babel-eslint

standard

snazzy
Run Code Online (Sandbox Code Playgroud)

请帮助了解如何修复它或如何在我的情况下正确运行 eslint。

先感谢您。

reactjs eslint create-react-app

6
推荐指数
2
解决办法
2万
查看次数

删除未使用的导入是否会对 Reactjs 中的包大小和构建时间产生影响

我有一个使用 Create React 应用程序创建的 Reactjs 应用程序,在启动和构建时会发出很多警告,指出我的组件中有未使用的变量或导入。

./src/components/home/Header.js
    Line 10:   'Switch' is defined but never used  no-unused-vars
    Line 10:   'Route' is defined but never used   no-unused-vars
Run Code Online (Sandbox Code Playgroud)

是否对no-unused-vars应用程序的最终包大小和构建时间产生相当大的影响。我可以通过删除所有这些警告来减少构建时间和包大小吗?

node.js npm reactjs webpack create-react-app

6
推荐指数
1
解决办法
6678
查看次数

为 facebook/create-react-app 生产版本手动设置 PORT 和 HOST

我使用 facebook/create-react-app 创建一个 React 应用程序,并成功在http://localhost:5000上运行生产构建。

我的问题是有什么方法可以更改此生产版本中的 5000 端口吗?

根据高级配置,在生产中无法更改 HOST 和 PORT,只能用于开发

https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#advanced-configuration。

这是我的 package.json

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "deploy": "serve -s build"
  }
Run Code Online (Sandbox Code Playgroud)

这是我的.env

##
HOST=127.0.0.1
PORT=8008
## PUBLIC_URL=http://myexamplesite.lk/
##

REACT_APP_ROOT_BASE_URL=https://myexamplesite.lk/api/
REACT_APP_ROOT_BASE_URL_META=http://myexamplesite.lk/
Run Code Online (Sandbox Code Playgroud)

reactjs react-scripts create-react-app

6
推荐指数
1
解决办法
1万
查看次数

如何在不弹出 create-react-app 的情况下压缩构建。还将压缩文件包含到 index.html 中的脚本标记中

我正在使用react-scripts build生产就绪构建并将其部署在我正在使用的 AWS 服务器上

GENERATE_SOURCEMAP=false yarn build && aws s3 sync build/ s3://*********

有什么方法可以应用 gzip 压缩来构建文件并将 gzip 压缩文件包含到 index.html 中并将其部署在 aws 上,以便在浏览器中 gzipped 文件将送达。

reactjs create-react-app

6
推荐指数
3
解决办法
1万
查看次数

React useEffect 内部的间隔 - 将其存储在 useRef Hook 中以保留值超时警告

所以我有这个方法:

useEffect(() => {
    //.. other logic here

    // Firefox doesn't support looping video, so we emulate it this way
    video.addEventListener(
      "ended",
      function() {
        video.play();
      },
      false
    );
  }, [videoWidth, videoHeight]);

Run Code Online (Sandbox Code Playgroud)

现在它抛出一个错误,它说:

Assignments to the 'interval' variable from inside React Hook useEffect will be lost after each render. To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property. Otherwise, you can move this variable directly inside useEffect.
Run Code Online (Sandbox Code Playgroud)

我很困惑这是什么意思?特别是这部分:To …

javascript intervals reactjs create-react-app react-hooks

6
推荐指数
1
解决办法
1万
查看次数

在开发模式下使用 create-react-app 时如何将应用程序放在子文件夹中?

这里有很多答案,其他地方的博客文章(包括官方文档https://facebook.github.io/create-react-app/docs/deployment#building-for-relative-paths)解释了如何构建你的 React 应用程序以便它可以从子文件夹运行。

但是,当我仍处于开发模式时,我找不到如何从子文件夹运行它的方法。(即不运行:npm run build)

我想在执行 npm start 时看到在 localhost:3000/web 上运行的应用程序。不是本地主机:3000。自动注入的静态资源(如

  • src="/static/js/bundle.js"

  • src="/static/js/1.chunk.js"

  • src="/static/js/main.chunk.js"

  • src="/main.947eb2055b7df4ce1a9e.hot-update.js"

    ) 应相应调整其路径以包含应用程序的子文件夹名称“web”。

是否可以?

我应该“弹出”应用程序并自己配置吗?

我应该克隆并更改“react-scripts start”吗?

有没有更简单的方法?

谢谢

更新:

我应该提供更多细节。我确实使用反向代理“http-proxy-middleware”,这就是我需要在子文件夹中运行每个应用程序的原因。下面使用 BrowserRouter 的“basename”的建议适用于链接,但不适用于注入的静态资源,例如“/static/js/bundle.js” 当您使用“basename”、“homepage”和运行方式:npm start。

因此,当您在端口 80 上的反向代理后面运行它时,它会尝试访问不存在的 localhost/static/js/bundle.js。它应该访问 localhost/web/static/js/bundle.js。但是由于这些链接是自动注入的,我无法控制添加“web”作为前缀。

reactjs react-router create-react-app

6
推荐指数
1
解决办法
3944
查看次数

如何使用 worker-loader 配置创建反应应用程序

我需要使用多个工作人员创建一个 CRA(create-react-app) 应用程序。例如(我的工作人员包括导入模块):

// worker.js
import x from 'file-xxx.js'
addEventListener('message', e => {
   // ...
})
Run Code Online (Sandbox Code Playgroud)
// App.jsx

App(){
   const mWorker = new Worker('./worker') 
   mWorker.postMessage('data...')

   // ...

}
Run Code Online (Sandbox Code Playgroud)

我想如果有人帮助我构建一个具有上述要求的简单 create react 应用程序。

目前我看到:

-- https://webpack.js.org/loaders/worker-loader/

-- https://github.com/GoogleChromeLabs/comlink

-- https://github.com/GoogleChromeLabs/worker-plugin

reactjs create-react-app worker-loader

6
推荐指数
1
解决办法
2134
查看次数

如何为 create-react-app --typescript 禁用 eqeqeq

我已经尝试了几件事,但我找不到如何为我的项目禁用 eqeqeq 的方法。

我的项目是通过 create-react-app --typescript 创建的

这是截图 https://imgur.com/RqC2D4i https://imgur.com/aKjJiyz

我已经为 eslint 或 tslint 尝试了几个插件

我尝试了几个配置文件:

.eslintrc
.eslint.config
.eslint.json
.tslintrc
.tslint.config
.tslint.json
Run Code Online (Sandbox Code Playgroud)

甚至在 package.json 中

"eslintConfig": {
  "extends": "react-app"
  "rules": {
    "eqeqeq": "off"
  }
},
Run Code Online (Sandbox Code Playgroud)

文件 rc、.json 或 .config 中的规则我尝试了几种组合

{
  "rules": {
    "eqeqeq": "off" // never, 0, 2
  }
}
Run Code Online (Sandbox Code Playgroud)

typescript create-react-app

6
推荐指数
1
解决办法
915
查看次数

如何在样式组件中注册多个字体粗细?

您好,我想使用不同字体粗细的 Inter 字体(400、500、700)。现在它只加载国米常规。我正在使用带有打字稿和样式组件的 create-react-app。

全局样式:

export const GlobalStyle = createGlobalStyle`
  @font-face {
     font-family: 'Inter';
     src: url(${Inter}) format("truetype"),
     url(${InterWoff}) format("woff"),
     url(${InterWoff2}) format("woff2");
     font-weight: normal;
     font-style: normal;
  };
  @font-face {
    font-family: 'Inter';
    src: url(${InterMedium}) format('truetype')
    url(${InterMediumWoff}) format('woff'),
    url(${InterMediumWoff2}) format('woff2');
    font-weight: 500;
    font-style: normal;
  };
  @font-face {
    font-family: 'Inter';
    src: url(${InterBold}) format('truetype')
    url(${InterBoldWoff}) format('woff'),
    url(${InterBoldWoff2}) format('woff2');
    font-weight: bold;
    font-style: normal;
  }
  body {
  margin: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans',
    'Droid Sans', 'Helvetica Neue', sans-serif;
  -webkit-font-smoothing: …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs create-react-app styled-components

6
推荐指数
1
解决办法
653
查看次数