英文:
Why am I getting error when trying to wrap my main component with another component that handles state?
问题
我有一个名为 Settings
的组件,负责切换明亮/暗黑模式。然而,在导航到另一个组件后,暗黑模式的状态不会保留(会回到明亮模式)。
我试图做的是在所有组件之间共享状态,而无需使用额外的库,通过使用 React Context 解决方案。
下面是我收到的错误,我不确定如何修复它。我已经尝试了很多种方法。任何建议将不胜感激。
警告:一个上下文消费者被渲染为多个子元素,或者一个不是函数的子元素。上下文消费者期望一个单一的函数子元素。如果您传递了一个函数,请确保其周围没有尾随或前导空白。
App.js
const App = () => {
return (
<>
<DarkModeContext.Consumer>
<Router>
<Switch>
<Route exact path="/" component={Home} />
<ProtectedRoute path="/settings" component={Settings} />
</Switch>
</Router>
</DarkModeContext.Consumer>
</>
);
}
const container = document.getElementById('example');
const root = createRoot(container);
root.render(<UserProvider><App/></UserProvider>);
Settings.js
import React, { useContext, useEffect } from 'react';
import Switch from '@mui/material/Switch';
import { FormControlLabel, Grid } from '@mui/material';
import { DarkModeContext } from "../../utilities/DarkModeContext";
const Settings = () => {
const { darkModeChecked, toggleDarkMode } = useContext(DarkModeContext);
useEffect(() => {
document.body.style.backgroundColor = darkModeChecked ? 'black' : 'white';
}, [darkModeChecked]);
return (
<Grid container direction="column" alignItems="center" justifyContent="center" spacing={2}>
<Grid item>
<FormControlLabel
control={<Switch checked={darkModeChecked} onChange={toggleDarkMode} />}
label="Text to the top"
labelPlacement="top"
/>
</Grid>
</Grid>
);
};
export default Settings;
DarkModeContext.js
import React, { createContext, useState } from 'react';
export const DarkModeContext = createContext();
export const DarkModeProvider = ({ children }) => {
const [darkModeChecked, setDarkModeChecked] = useState(true);
const toggleDarkMode = () => {
setDarkModeChecked((prev) => !prev);
};
return (
<DarkModeContext.Provider value={{ darkModeChecked, toggleDarkMode }}>
{children}
</DarkModeContext.Provider>
);
};
英文:
I have a Settings
component which handles toggling light/dark mode. However, upon navigating to another component, the state of the dark mode doesn't persist (it goes back to light mode).
What I'm trying to do is share the state across all components without the need for additional libraries by using the state management solution React Context.
This is the error I'm getting below and I'm not sure how to fix it. I've tried so many ways. Any suggestions would be appreciated.
Warning: A context consumer was rendered with multiple children, or a child that isn't a function. A context consumer expects a single child that is a function. If you did pass a function, make sure there is no trailing or leading whitespace around it.
App.js
const App = () => {
return (
<>
<DarkModeContext.Consumer>
<Router>
<Switch>
<Route exact path="/" component={Home} />
<ProtectedRoute path="/settings" component={Settings} />
</Switch>
</Router>
</DarkModeContext.Consumer>
</>
);
}
const container = document.getElementById('example');
const root = createRoot(container);
root.render(<UserProvider><App/></UserProvider>);
Settings.js
import React, { useContext, useEffect } from 'react';
import Switch from '@mui/material/Switch';
import { FormControlLabel, Grid } from '@mui/material';
import {DarkModeContext} from "../../utilities/DarkModeContext";
const Settings = () => {
const { darkModeChecked, toggleDarkMode } = useContext(DarkModeContext);
useEffect(() => {
document.body.style.backgroundColor = darkModeChecked ? 'black' : 'white';
}, [darkModeChecked]);
return (
<Grid container direction="column" alignItems="center" justifyContent="center" spacing={2}>
<Grid item>
<FormControlLabel
control={<Switch checked={darkModeChecked} onChange={toggleDarkMode} />}
label="Text to the top"
labelPlacement="top"
/>
</Grid>
</Grid>
);
};
export default Settings;
DarkModeContext.js
import React, { createContext, useState } from 'react';
export const DarkModeContext = createContext();
export const DarkModeProvider = ({ children }) => {
const [darkModeChecked, setDarkModeChecked] = useState(true);
const toggleDarkMode = () => {
setDarkModeChecked((prev) => !prev);
};
return (
<DarkModeContext.Provider value={{ darkModeChecked, toggleDarkMode }}>
{children}
</DarkModeContext.Provider>
);
};
答案1
得分: 1
这不是正确使用 Context.Consumer
的方式。警告告诉您问题所在。它期望一个返回 React 元素的函数。
<Context.Consumer>{(thingsFromContextValue) => <Component />}</Context.Consumer>
您目前的用法没有意义。Consumer 用于提供返回值,如果您不使用该值,就不需要使用 Consumer。
另外,我看不到您在哪里使用了您的 DarkModeProvider
。它必须包裹那些使用该上下文的部分。
英文:
This is not the right way of using Context.Consumer
. The warning tells you what the problem is. It expects function that return ReactElement.
<Context.Consumer>{(thingsFromContextValue) => <Component />}</Context.Consumer>
The way you using it does not make sense. It is there to give the return value, if you are not using that value, no need to use the Consumer.
Also I don't see where you use your DarkModeProvider
. It has to wrap those that use that context
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论