标签: create-react-app

从 Create-React-App 升级到 Next.js - CSS 样式表不起作用

最近我们发现我们的 React 项目必须使用 SSR。我检查了我所知道的每一种方法,并几乎测试了我在媒体和其他网站上找到的所有方法。经过大量工作后,我决定我们必须迁移到 Next JS。

虽然迁移过程一切都很好,但对于样式表而言。

在我们应用程序的旧版本中,我们使用 webpack 将样式与项目捆绑在一起,一切都很好。

这是 webpack.config.js

const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const port = process.env.PORT || 3000;
const extractSCSS = new ExtractTextPlugin('./[name].css');

// const UglifyJS = require('uglifyjs-webpack-plugin');
module.exports = {

  mode: 'development',
  output: {
    filename: 'bundle.[hash].js',
    publicPath: '/'
  },
  devtool: 'source-map',
  module: {
    rules: [

      // First Rule

      {
        test: /\.(js)$/,
        exclude: /node_modules/,
        use: ['babel-loader'],

      },

      // Second Rule

      {
        test: /\.scss$/,
        use: ['css-hot-loader'].concat(extractSCSS.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader?sourceMap', …
Run Code Online (Sandbox Code Playgroud)

reactjs server-side-rendering create-react-app next.js

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

我无法在 create-react-app 中配置代理

我是React初学者,正在基于React文档开发一个项目。

后端api服务器创建为node.js并由PM2管理为http://localhost:4005。前端使用create-react-app创建项目。

为了参考后端的api,我参考这个手册进行了代理设置。

我尝试了 package.json 代理配置和使用 http-proxy-middleware 的手动配置,但没有成功。

代理已配置,但 webpack 开发服务器会调用自身,如下所示。

在此输入图像描述

纱线启动消息(反应)

You can now view client in the browser.

  Local:            http://localhost:63323/
  On Your Network:  http://172.30.1.13:63323/

Note that the development build is not optimized.
To create a production build, use yarn build.

[HPM] Error occurred while trying to proxy request /api/getList from localhost:63323 to http://localhost:4005/ (ETIMEDOUT) (https://nodejs.org/api/errors.html#errors_common_system_errors)
Run Code Online (Sandbox Code Playgroud)

浏览器控制台消息:

List.js:19 GET http://localhost:63323/api/getList 
net::ERR_CONNECTION_REFUSED
list:1 Uncaught (in promise) TypeError: Failed to fetch
Run Code Online (Sandbox Code Playgroud)

(63323端口是react指定的端口。)

我的项目目录如下所示:

在此输入图像描述

Express 服务器index.js

const express = require('express'); …
Run Code Online (Sandbox Code Playgroud)

javascript proxy reactjs webpack create-react-app

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

通过使用反应选择映射对象数组来生成选项

我正在使用react-select在我的中创建一个选择选项create-react-app,并尝试映射对象数组以生成选项。我的应用程序加载正常,但当我单击“选择”时,出现此错误:Uncaught Invariant Violation: Objects are not valid as a React child (found: object with keys {name}). If you meant to render a collection of children, use an array instead.

\n\n

我通过 props 将数据传递给组件,该组件工作正常,数据的结构如下:

\n\n
const guests = [\n    {\n        name: 'Kait',\n        plus: true,\n        plusName: 'Kitty'\n    },\n    {\n        name: 'S\xc3\xa9anin',\n        plus: true,\n        plusName: 'Guest'\n    }\n]\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是选择组件:

\n\n
<Select\n   value={selectedOption}\n   onChange={this.handleChange}\n   options={\n      this.props.guests.map((guest, index) => {\n         return {\n            label: guest,\n            value: guest,\n            key: index\n         }\n      })\n …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-select create-react-app

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

在使用 Create-React-App 时,我们应该导入整个库还是特定的单个组件?

我问的是性能/优化问题。我意识到所有库(及其默认导出方法)都是不同的,但例如在react-bootstrap的文档介绍中它指出

您应该导入单个组件,例如:react-bootstrap/Button,而不是整个库。这样做只会引入您使用的特定组件,这可以显着减少您最终发送到客户端的代码量。

既然已经到了 2019 年,Create-React-App (CRA) 的构建是否会自动为我们优化,并且只导入那些至少使用过一次的组件?

如果是这样,这是否意味着我们可以忽略建议并导入整个库而不是特定组件?

javascript deployment reactjs react-native create-react-app

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

使用 NODE_MODULE_VERSION 64 针对不同的 Node.js 版本进行编译。此版本的 Node.js 需要 NODE_MODULE_VERSION 67

npm安装成功。npm start 抛出以下错误。

不依赖 bcrpt 和 node-pre-gyp 关注了类似问题的大多数评论,例如清理缓存、重建、删除节点模块并再次安装,但没有一个对我有用。Node版本有问题吗?节点:11.15.0 npm:6.7.0

** npm start 上的错误消息 **

Module build failed (from ../node_modules/sass-loader/lib/loader.js):
Error: The module '\\?\D:\_Install\win32-x64-64_binding.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 64. This version of Node.js requires
NODE_MODULE_VERSION 67. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:857:18)
    at Module.load (internal/modules/cjs/loader.js:685:32)
    at Function.Module._load (internal/modules/cjs/loader.js:620:12)
    at Module.require (internal/modules/cjs/loader.js:723:19)
    at require (internal/modules/cjs/helpers.js:14:16)
    at module.exports (C:\Users\syedabu.c\Downloads\ReactJSBlueprint\node_modules\node-sass\lib\binding.js:19:10)
    at Object.<anonymous> (C:\Users\syedabu.c\Downloads\ReactJSBlueprint\node_modules\node-sass\lib\index.js:14:35)
    at Module._compile (internal/modules/cjs/loader.js:816:30)
    at …
Run Code Online (Sandbox Code Playgroud)

node.js reactjs node-sass sass-loader create-react-app

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

如何将 React 组件导出为 npm 包?Create-React-App 语法错误:意外的标记

我创建了反应应用程序,名称为“create-react-app-npm”。在 ./src/index.js 文件中,我导出了组件以在另一个项目中使用它的 npm 包。首先,我在我的主根目录中运行了这段代码。

npm run eject然后是我项目的下一部分

这是create-react-app-npm文件夹中的src/index.js

import React from 'react';
import './index.css';
import App from './App';

class ExportApp extends React.Component{
    render(){
        return(<App sayHi="hello There I am creating my first npm package"/>);
    }
}
export default ExportApp;


Run Code Online (Sandbox Code Playgroud)

这是 create-react-app-npm 文件夹中的 package.json 文件

{
  "name": "create-react-app-npm",
  "version": "0.1.0",
  "main": "src/index.js",
  "dependencies": {
    "@babel/core": "7.5.5",
    "@svgr/webpack": "4.3.2",
    "@typescript-eslint/eslint-plugin": "1.13.0",
    "@typescript-eslint/parser": "1.13.0",
    "babel-eslint": "10.0.2",
    "babel-jest": "^24.8.0",
    "babel-loader": "8.0.6",
    "babel-plugin-named-asset-import": "^0.3.3",
    "babel-preset-react-app": "^9.0.1",
    "camelcase": "^5.2.0",
    "case-sensitive-paths-webpack-plugin": "2.2.0",
    "css-loader": "2.1.1",
    "dotenv": "6.2.0", …
Run Code Online (Sandbox Code Playgroud)

javascript npm reactjs webpack create-react-app

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

eslint vscode 插件不会为 hooks 生成警告

我创建了一个反应应用程序并npx create-react-app my-app进行了纱线安装。在 App.js 中添加了以下内容:

const a = () => 1,
  b = () => 2,
  c = () => 3;

function App() {
  const wut = useMemo(() => {
    return { a: a(), b: b(), c: c() };
  }, [a]);
  useEffect(() => {
    console.log(a(), b(), c());
  }, [a]);
Run Code Online (Sandbox Code Playgroud)

Yarn start 会给我警告,但 vscode 不会。

我添加.eslintrc.js了以下内容:

module.exports = {
  env: {
    browser: true,
    es6: true
  },
  extends: ["eslint:recommended", "plugin:react/recommended"],
  globals: {
    Atomics: "readonly",
    SharedArrayBuffer: "readonly"
  },
  parserOptions: …
Run Code Online (Sandbox Code Playgroud)

reactjs eslint visual-studio-code create-react-app

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

删除旧版本的 create-react-app

我想使用该脚本创建一个新的反应应用程序create-react-app。我仍然安装了一个不再受支持的全局版本,因此我卸载了全局版本并尝试创建一个新版本,如下所示:

npx create-react-app myapp
Run Code Online (Sandbox Code Playgroud)

然后我收到一个错误,提示没有使用模板,并且我可能使用旧版本的 create-react-app。我在网上阅读,这确实有效:

npx --ignore-existing create-react-app myapp
Run Code Online (Sandbox Code Playgroud)

我读到这意味着即使我确实卸载了全局版本,我仍然拥有旧版本。那么如何删除其他旧版本的 create-react-app 呢?

npm reactjs create-react-app npx

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

如何在生产中删除 Create React 应用程序示例

我有一个已成功部署在生产服务器中的域。但每当我打开它时,Install app: Create React app sample地址栏中都会显示一个。

有什么办法可以去掉这个吗。我应该在我的代码中更改什么?这里有什么建议吗?

//客户端包.json

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "axios": "^0.19.2",
    "bootstrap": "^4.4.1",
    "bootstrap-less": "^3.3.8",
    "moment": "^2.24.0",
    "node-sass": "^4.13.1",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "redux-thunk": "^2.3.0",
    "uuid": "^7.0.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
    ],
    "development": [
      "last 1 chrome version",
    ]
  },
  "proxy": "http://localhost:5000"
}
Run Code Online (Sandbox Code Playgroud)

//清单.json

{
  "short_name": "React App",
  "name": "Create React App Sample",
  "icons": …
Run Code Online (Sandbox Code Playgroud)

production-environment reactjs create-react-app

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

将实用程序脚本添加到 create-react-app React / Typescript 应用程序

我有一个使用 Typescript 创建的单页 React 应用程序create-react-app --typescript。源代码位于.ts,.tsx文件位于src/.

现在我想创建一些实用程序脚本以在开发中使用。在本例中,从我们的生产服务器下载数据并将其上传到我的本地开发服务器,但这只是一个示例。这些脚本具有应用程序也具有的依赖项(如 API 客户端库),我想从应用程序导入各种常量和辅助函数。

在 create-react-app 的结构中是否有一个地方可以放置此类脚本并让它们工作?我希望能够打电话给他们npm run scriptname并让他们从应用程序导入。

(在我们的 Django 后端,我会将这些放入 Django 管理命令中,我正在寻找一种方法来制作类似的东西)

javascript typescript create-react-app

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