[英]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.