react-select 实现的钩子调用无效

问题描述 投票:0回答:5

我尝试在代码中使用react-select,但我不断收到此错误:

Error: 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:

我该怎么办?这是反应选择的链接https://react-select.com/home#animated-components

import Select from 'react-select';

const options = [

  { value: '1', label: '1' },
  { value: '2', label: '2' },
  { value: '3', label: '3' },
  { value: '4', label: '4' },
  { value: '5', label: '5' },
  { value: '6', label: '6' },
  { value: '7', label: '7' },
  { value: '8', label: '8' },
  { value: '9', label: '9' },
]


function Sound() {
  return (
    <div className="Sound">
      <Select options={options} />
    </div>
  );
}

export default Sound;
javascript reactjs react-hooks react-select
5个回答
1
投票

您的代码看起来很完美。只需检查您是否已安装

react-select

您可以简单地使用它来安装 React Select

npm i --save react-select

这是在本地检查的。工作完美。


0
投票

如果您分享的是整个代码,那么可能是因为您没有导入react。因此它认为这是一个不接受钩子的 JavaScript 函数。


0
投票

可能有点晚了,但就我而言,错误的根源是我从项目中安装了一个目录,刚刚进入项目目录并执行了

npm install react-select
,还删除了在中创建的package.json和节点模块项目父目录。


0
投票

就我而言,我将

react-select
安装在错误的目录中。

我只需删除刚刚在错误目录中创建的

node_modules
文件夹、
package-lock.json
package.json
文件,然后在正确的目录中重新安装“react-select”

注意:注意只删除那些在错误目录中创建的文件。
换句话说,永远不要删除原始

node_modules
文件夹、
package-lock.json
package.json
文件


0
投票

迟到了,但由于尝试安装在错误的目录中,而不是反应项目目录中,我收到了此错误。我正在使用 django,我正在这个文件夹上下载

© www.soinside.com 2019 - 2024. All rights reserved.