小编Twi*_*ist的帖子

带有 hooks 的 React 组件作为 npm 模块

当我尝试在用作依赖项的模块中使用 React Hooks 时,它失败了。我收到以下错误:

Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
Run Code Online (Sandbox Code Playgroud)

但如果我使用基于类的组件,那就没问题了。为什么会发生这种情况?

这是我的摘录package.json

{
  "main": "dist/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "./node_modules/.bin/babel src -d dist",
    "prepublish": "npm run build"
  },
  "peerDependencies": {
    "react": "^16.10.2",
    "react-router-dom": "^5.1.2"
  },
  "devDependencies": {
    "axios": "^0.19.0",
    "jwt-decode": "^2.2.0",
    "qs": "^6.9.0",
    "@babel/cli": "^7.6.4",
    "@babel/core": "^7.6.4",
    "@babel/preset-env": "^7.6.3",
    "@babel/preset-react": "^7.6.3"
  } …
Run Code Online (Sandbox Code Playgroud)

npm reactjs npm-publish react-hooks

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

VS Code 扩展以突出显示重复的 css 属性

任何 VS Code 扩展来突出显示重复的 css 属性?例如

.test {
  font-size: 14px;
  ...
  font-size: 10px;
}
Run Code Online (Sandbox Code Playgroud)

css syntax-highlighting visual-studio-code

5
推荐指数
1
解决办法
2558
查看次数

如果对象有数字键,是否有任何现代方法可以在 Object.assign 中禁用排序?

例如:片段 img

var a = {1: '1', 2: '2'}
var b = {3: '3', 4: '4'}

Object.assign({}, a, b)
> {1: "1", 2: "2", 3: "3", 4: "4"}

Object.assign({}, b, a)
> {1: "1", 2: "2", 3: "3", 4: "4"}
Run Code Online (Sandbox Code Playgroud)

有没有办法禁用排序?

javascript ecmascript-5 ecmascript-6

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

使用全局 onRequest 处理程序在 nuxt3 中重新定义 $fetch

是否可以使用全局 onRequest 处理程序通过 Nuxt3 进行 $fetch,以在每个请求上添加特定数据?使用 nuxt2 和 axios 就很简单

/plugins/axios.js

export default function ({ $axios, store, req }) {
  $axios.onRequest((config) => {
    if (config.data) {
      config.data.test = '123';
    } else {
      config.data = { test: '123' };
    }
    return config;
  });
}
Run Code Online (Sandbox Code Playgroud)

但是如何在 Nuxt3 和 $fetch 上实现相同的目标呢?

fetch vue.js nuxt.js nuxt3

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