试图将 DefaultLayout 组件设置为初始状态值。它抛出无法解构未定义的属性子项。
如果我将 DefaultLayout 组件包装在 React.memo 中,它可以正常工作,即导出默认 React.memo(DefaultLayout)
任何人都可以解释这种行为的原因。
请找到沙盒链接
https://codesandbox.io/s/autumn-firefly-qp5gh?file=/pages/index.js
布局.js
import React, { useEffect, useState } from 'react'
import { AppLayout } from 'utilities/appComponentConfig'
import { useGlobalUI } from 'store/GlobalUI'
import { APP_TYPE } from 'utilities/Constants'
import { fetchKioskInfo } from '../services/kiosk'
const Layout = ({ Pages, pageProps, cookie }) => {
const { setAppType, setAgentCookie } = useGlobalUI()
const [Component, setComponent] = useState(AppLayout[APP_TYPE.WEB])
setAgentCookie(cookie)
useEffect(() => {
const fetchKiosk = async () => {
const kioskDetails = await fetchKioskInfo(cookie)
if (kioskDetails?.length > 0) {
setAppType(APP_TYPE.KIOSK)
return setComponent(AppLayout[APP_TYPE.KIOSK])
} else {
setAppType(APP_TYPE.WEB)
return setComponent(AppLayout[APP_TYPE.WEB])
}
}
if(cookie?.includes('KIOSK'))fetchKiosk()
}, [])
return (
<>
{Component ? (
<Component pageProps={pageProps}>
<Pages {...pageProps} />
</Component>
) : null}
</>
)
}
export default Layout
appComponentConfig.js
import { APP_TYPE } from './Constants'
import DefaultLayout from 'layouts/DefaultLayout'
import KioskLayout from 'layouts/KioskLayout'
const AppLayout = {
[APP_TYPE.WEB]: DefaultLayout,
[APP_TYPE.KIOSK]: KioskLayout,
}
export { AppLayout }
DefaultLayout.js
const DefaultLayout = ({ children, pageProps }) => {
const mainNode = pageProps.main || {}
const settings = mainNode.settings || {}
const showFooter = !settings.hideFooter
const errorDetail = _find(pageProps, (item) => item.error && item.status)
const router = useRouter()
return (
<>
<div style={{ display: 'none' }} className="version">
Version 1.0.5
</div>
</div>
</>
)
}
export default DefaultLayout
_app.js
import React, { useEffect } from 'react'
import { useCookie } from 'next-cookie'
import '../styles/main.css'
import UserProvider from '../store/User'
import PageProvider from '../store/Page'
import GlobalUIProvider from '../store/GlobalUI'
import BookingProvider from '../store/Booking'
import ExtraProvider from '../store/Extras'
import CartProvider from '../store/Cart'
import CheckoutProvider from '../store/Checkout'
import { useRouter } from 'next/router'
import { storePathValues } from '../utilities/helperFunctions'
import Layout from 'layouts/Layout'
const App = ({ Component: Pages, pageProps, cookie }) => {
const router = useRouter()
return (
<>
<PageProvider model={pageProps}>
<GlobalUIProvider>
<UserProvider>
<CartProvider>
<CheckoutProvider>
<BookingProvider>
<ExtraProvider>
<Layout
Pages={Pages}
pageProps={pageProps}
cookie={agentCookie}
/>
</ExtraProvider>
</BookingProvider>
</CheckoutProvider>
</CartProvider>
</UserProvider>
</GlobalUIProvider>
</PageProvider>
</>
)
}
export default App