2 回答
TA贡献1845条经验 获得超8个赞
不能在放置提供程序的同一位置使用上下文值。因此,您可以在两个文件中分开:
// _app.tsx
import { ThemeProvider } from '../contexts/theme';
import Container from './_container';
import type { AppProps } from 'next/app';
function MyApp(appProps: AppProps) {
return (
<ThemeProvider>
<Container {...appProps} />
</ThemeProvider>
);
}
// _container.tsx
import type { AppProps } from 'next/app';
import { ThemeProvider as SCThemeProvider } from 'styled-components';
import { defaultColors, useTheme } from '../contexts/theme';
const Container = ({ Component, pageProps }: AppProps) => {
const { colors } = useTheme();
return (
<>
<SCThemeProvider theme={{ colors: colors ? colors : defaultColors }}>
<Component {...pageProps} />
</SCThemeProvider>
</>
);
};
export default Container;
TA贡献1863条经验 获得超2个赞
也许是这样的:
# File: app-context.ts
const AppContext = React.createContext({
user: null,
isAuthenticated: false,
setUser: null,
});
export default AppContext;
# File: _app.tsx
import AppContext from "./app-context";
export default class MyApp extends App {
render() {
const { Component, pageProps } = this.props;
return (
<AppContext.Provider
value={{
user: "todo",
isAuthenticated: false,
setUser: "todo",
}}
>
<Component {...pageProps} />
</AppContext.Provider>
);
}
}
添加回答
举报