Chakra 在 pages 文件夹中运行良好,但是当尝试在 components 文件夹中使用它时,问题就开始了。
我添加ChakraProvider
了识别属性,到目前为止一切都很好,但是Accordion
不起作用,让手风琴在自己的组件上工作的正确方法是什么?
我有以下文件夹结构:
源代码
- 资产
- 成分
- 标题
- 页脚
- 挂钩
- 页面
- 服务
- 风格
"@chakra-ui/react": "^1.3.4",
"next": "10.0.8",
"react": "17.0.1",
页面/app.tsx
import { ChakraProvider } from '@chakra-ui/react';
import {AppProps} from 'next/app'
import guiaTheme from '../styles/theme';
import GlobalStyle from '../styles/global'
const MyApp: React.FC<AppProps> = ({ Component, pageProps }) => {
return(
<ChakraProvider theme={guiaTheme}>
<Component {...pageProps} />
<GlobalStyle/>
</ChakraProvider>
)
}
export default MyApp
页面/_document.tsx
import Document, { Html, Head, Main, NextScript, DocumentInitialProps, DocumentContext } from 'next/document'
import { ServerStyleSheet } from 'styled-components'
import Header from '../components/Header'
import Footer from '../components/Footer'
class MyDocument extends Document {
static async getInitialProps(ctx: DocumentContext): Promise<DocumentInitialProps> {
const sheet = new ServerStyleSheet()
const originalRenderPage = ctx.renderPage
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) =>
sheet.collectStyles(<App {...props} />),
})
const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
),
}
} finally {
sheet.seal()
}
}
render() {
return (
<Html lang="pt">
<Head />
<body>
<Header/>
<Main />
<Footer/>
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
这就是问题所在。组件/页脚/index.tsx
import { ChakraProvider, Center, Img, Box, HStack, VStack, Heading } from '@chakra-ui/react'
import {Accordion, AccordionItem, AccordionButton, AccordionIcon, AccordionPanel} from '@chakra-ui/accordion'
import guiaTheme from '../../styles/theme';
function Footer() {
return (
<ChakraProvider theme={guiaTheme}>
<Box as="footer" bg="guiared.600">
<VStack>
<Center py={2}>
<Img src="header-logo.png" alt="Teste" height="39" />
</Center>
<HStack justify="center">
<Img src="/appstore-icon.png" srcSet="/appstore-icon.png 1x, /appstore-icon@2x.png 2x" />
<Img src="/googleplay-icon.png" srcSet="/googleplay-icon.png 1x, /googleplay-icon@2x.png 2x" />
</HStack>
</VStack>
<Accordion>
<AccordionItem>
<Heading>
<AccordionButton>
<Box flex="1" textAlign="left">
Section 1 title
</Box>
<AccordionIcon />
</AccordionButton>
</Heading>
<AccordionPanel pb={4}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat.
</AccordionPanel>
</AccordionItem>
<AccordionItem>
<h2>
<AccordionButton>
<Box flex="1" textAlign="left">
Section 2 title
</Box>
<AccordionIcon />
</AccordionButton>
</h2>
<AccordionPanel pb={4}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat.
</AccordionPanel>
</AccordionItem>
</Accordion>
</Box>
</ChakraProvider>
);
}
export default Footer;