最近我们发现我们的 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)我是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) 我正在使用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.
我通过 props 将数据传递给组件,该组件工作正常,数据的结构如下:
\n\nconst 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]\nRun 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) 我问的是性能/优化问题。我意识到所有库(及其默认导出方法)都是不同的,但例如在react-bootstrap的文档介绍中它指出
您应该导入单个组件,例如:react-bootstrap/Button,而不是整个库。这样做只会引入您使用的特定组件,这可以显着减少您最终发送到客户端的代码量。
既然已经到了 2019 年,Create-React-App (CRA) 的构建是否会自动为我们优化,并且只导入那些至少使用过一次的组件?
如果是这样,这是否意味着我们可以忽略建议并导入整个库而不是特定组件?
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) 我创建了反应应用程序,名称为“create-react-app-npm”。在 ./src/index.js 文件中,我导出了组件以在另一个项目中使用它的 npm 包。首先,我在我的主根目录中运行了这段代码。
npm run eject然后是我项目的下一部分
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)
{
"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) 我创建了一个反应应用程序并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) 我想使用该脚本创建一个新的反应应用程序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 呢?
我有一个已成功部署在生产服务器中的域。但每当我打开它时,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) 我有一个使用 Typescript 创建的单页 React 应用程序create-react-app --typescript。源代码位于.ts,.tsx文件位于src/.
现在我想创建一些实用程序脚本以在开发中使用。在本例中,从我们的生产服务器下载数据并将其上传到我的本地开发服务器,但这只是一个示例。这些脚本具有应用程序也具有的依赖项(如 API 客户端库),我想从应用程序导入各种常量和辅助函数。
在 create-react-app 的结构中是否有一个地方可以放置此类脚本并让它们工作?我希望能够打电话给他们npm run scriptname并让他们从应用程序导入。
(在我们的 Django 后端,我会将这些放入 Django 管理命令中,我正在寻找一种方法来制作类似的东西)
create-react-app ×10
reactjs ×9
javascript ×5
npm ×2
webpack ×2
deployment ×1
eslint ×1
next.js ×1
node-sass ×1
node.js ×1
npx ×1
proxy ×1
react-native ×1
react-select ×1
sass-loader ×1
typescript ×1