简体   繁体   English

TypeError: Object(...) is not a function 错误在 useAccordianToggle

[英]TypeError: Object(…) is not a function error in useAccordianToggle

I'm new to react.我是新来的反应。 I'm using react-bootstrap and trying to create an accordion having more than 20 cards in it.我正在使用 react-bootstrap 并尝试创建一个包含 20 多张卡片的手风琴。 Each card header has a right/down pointing caret icon.每张卡片 header 都有一个右/下指向插入符号图标。 I want to toggle the icon to point down or point right based on whether the card body is collapsed or not.我想根据卡体是否折叠将图标切换为向下或向右。 So, i tried implementing the example from react-bootstrap website Custom Toggle with Expansion Awaremess and I'm getting the above error at the line所以,我尝试从 react-bootstrap 网站Custom Toggle with Expansion Awaremess实现示例,我在该行遇到了上述错误

 const decoratedOnClick = useAccordionToggle(
    eventKey,
    () => callback && callback(eventKey),
    ); 

The complete code is below.完整的代码如下。

    import React, {useContext} from 'react';
    import Accordion from 'react-bootstrap/Accordion';
    import useAccordionToggle from 'react-bootstrap/AccordionToggle';
    import AccordionContext from 'react-bootstrap/AccordionContext';
    import Card from 'react-bootstrap/Card';
    import 'bootstrap/dist/css/bootstrap.min.css';

    function ContextAwareToggle({ children, eventKey, callback }) {
    const currentEventKey = useContext(AccordionContext);

    const decoratedOnClick = useAccordionToggle(
    eventKey,
    () => callback && callback(eventKey),
    );

   const isCurrentEventKey = currentEventKey === eventKey;

   return (
    <button
      type="button"
      style={{ backgroundColor: isCurrentEventKey ? 'pink' : 'lavender' }}
      onClick={decoratedOnClick}
    >
      {children}
    </button>
  );
}

function App() {
  return (
    <Accordion defaultActiveKey="0">
    <Card>
      <Card.Header>
        <ContextAwareToggle eventKey="0">Click me!</ContextAwareToggle>
      </Card.Header>
      <Accordion.Collapse eventKey="0">
        <Card.Body>Hello! I'm the body</Card.Body>
      </Accordion.Collapse>
    </Card>
    <Card>
      <Card.Header>
        <ContextAwareToggle eventKey="1">Click me!</ContextAwareToggle>
      </Card.Header>
      <Accordion.Collapse eventKey="1">
        <Card.Body>Hello! I'm another body</Card.Body>
      </Accordion.Collapse>
    </Card>
  </Accordion>
);
}


export default App;

I even tried setting my own context like below我什至尝试设置自己的上下文,如下所示

import React from 'react';

export default React.createContext(null);

But I'm still getting the same error!但我仍然遇到同样的错误!

Also, i have come across examples of using css to achieve this but i do not have a choice of using different libraries like fontawesome.此外,我遇到了使用 css 来实现此目的的示例,但我无法选择使用不同的库,例如 fontawesome。 Is there a better way to implement what I'm looking for?有没有更好的方法来实现我正在寻找的东西? Thanks.谢谢。

I figured it out and it was a silly mistake.我想通了,这是一个愚蠢的错误。 i had to use {} around useAccordianToggle import statement as below.我必须在 useAccordianToggle 导入语句周围使用 {},如下所示。

import { useAccordionToggle } from 'react-bootstrap/AccordionToggle';

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

相关问题 ReactJS 错误类型错误:对象(…)不是 function - ReactJS error TypeError: Object(…) is not a function Ruby on Rails with React“TypeError: Object(...) is not a function”错误 - Ruby on Rails with React "TypeError: Object(...) is not a function" Error Coffeescript未捕获的TypeError:对象不是函数错误 - Coffeescript Uncaught TypeError: object is not a function Error “未捕获的TypeError:对象不是函数” javascript中的错误 - “Uncaught TypeError: object is not a function” Error in javascript jQuery错误 - 未捕获TypeError:对象不是函数 - jQuery error - Uncaught TypeError: object is not a function Angular printjs: ERROR TypeError: Object(...) is not a function - Angular printjs: ERROR TypeError: Object(...) is not a function Javascript错误,未捕获TypeError属性&#39;$&#39;的对象[object Object]不是函数 - Javascript Error, Uncaught TypeError Property '$' of object [object Object] is not a function 奇怪的(Webpack?)错误“ TypeError:Object(…)不是函数” - Strange (webpack?) error “TypeError: Object(…) is not a function” 错误:类型错误:在对象函数 mergeAcross 中找不到函数 getValue - Error:TypeError: Cannot find function getValue in object function mergeAcross 未捕获的TypeError:对象不是函数? - Uncaught TypeError: object is not a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM