简体   繁体   English

Rest 参数在我的 react-bootstrap 组件中产生错误?

[英]Rest parameters produces an error in my react-bootstrap component?

This always works for me but for some reason, it does not work in my react-bootstrap component and this is confusing, rest parameter {classes, ...props} cause the code to throw an error and don't know why??这总是对我有用,但由于某种原因,它在我的 react-bootstrap 组件中不起作用,这令人困惑,rest 参数{classes, ...props}导致代码抛出错误并且不知道为什么?

When i tried to reproduce the error in codesandbox everything worked ok, but the error still in my machine even after creating many new projects and removing all packages that can affect code till i reached to the point that i have created a very basic project i uploaded it on github here当我尝试在codesandbox中重现错误时,一切正常,但即使在创建了许多新项目并删除了所有可能影响代码的包之后,错误仍然存在于我的机器中,直到我创建了一个我上传的非常基本的项目它在github

Just importing the ReusableDropdown component from dropdown.js file produced a type error and react says that the error happens in the component itself not inside the parent component:只是从dropdown.js文件中导入ReusableDropdown组件会产生类型错误,并且反应说错误发生在组件本身而不是父组件内部:

./src/reusable/dropdown.js
TypeError: Cannot read property 'forEach' of undefined

This should work and i am sure the error has no relation to the syntax, that's a kind of weird error!!这应该有效,我确信错误与语法无关,这是一种奇怪的错误!

PS: You need to install react-bootstrap and bootstrap as dependencies to be able to reproduce the error PS:需要安装 react-bootstrap 和 bootstrap 作为依赖才能重现错误

It could be an off-topic, but, since I saw there is no classes in <ReusableDropdown> component,这可能是一个题外话,但是,因为我看到<ReusableDropdown>组件中没有classes

<ReusableDropdown text="his"/>

I want to suggest adding defaultProps like following我想建议添加 defaultProps 如下

import React, { Component } from 'react';
import { Dropdown } from 'react-bootstrap';

export default ({classes = [], ...props}) => {
  return (
    <Dropdown>
       <Dropdown.Toggle className={classes.toggle}>{props.text}</Dropdown.Toggle>
       <Dropdown.Menu>
         <Dropdown.Item>hi</Dropdown.Item>
       </Dropdown.Menu>
    </Dropdown>
  );
}

Or, you can check null value或者,您可以检查 null 值

<Dropdown>
  <Dropdown.Toggle className={classes && classes.toggle}>
    {props.text}
  </Dropdown.Toggle>
  <Dropdown.Menu>
    <Dropdown.Item>hi</Dropdown.Item>
  </Dropdown.Menu>
</Dropdown>

And, I'm understanding that Dropdown.Toggle requires id prop.而且,我了解Dropdown.Toggle需要id道具。 Could you check required props as well?你能检查一下所需的道具吗?

@Jaromanda X @Misol Goh Thanks for trying to help @Jaromanda X @Misol Goh 感谢您的帮助

after many hours trying to debug, i found this thread that answered my question经过几个小时尝试调试后,我发现这个线程回答了我的问题

Cannot read property 'forEach' of undefined in React 无法读取 React 中未定义的属性“forEach”

Solution 1:解决方案1:

adding the following to package.json:将以下内容添加到 package.json:

"resolutions": {
    "eslint-loader": "3.0.2"
  }

Solution 2:解决方案2:

downgrade to 3.0.1 version of react-scripts as the newer version caused that error for many users降级到 react-scripts 的 3.0.1 版本,因为较新的版本导致许多用户出现该错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM