useContext 给我的 React router dom 带来了问题
所以我有这个应用程序文件用于反应,
const [cart, setCart] = useState([
])
return (
<Router>
<Routes>
<Route element={<Layout />}>
<cartContext.Provider value={{ cart, setCart }}>{/* you wrap provider around the components that you want to have access to the state */}
<Route path="/" index element={<Home/>} />
<Route path="Bracelets" element={<Bracelets />} />
<Route path="Signup" element={<Signup />} />
<Route path="Login" element={<Login />} />
<Route path="Cart" element={<Cart />} />
<Route path="Cart/shipping-buynow" element={<ShippingAddToCart />} />
<Route path="/help" element={<Help />} />
<Route path="/Modal" element={<Modal />} />
<Route path="/Add" element={<Add />} />
{/* <Route path="/create" element={<CreateMeet/>} /> */}
{/* <Route path="/Register" element={<Register />} /> */}
</cartContext.Provider>
</Route>
</Routes>
</Router>
)
}
但由于某种原因,我实现的 useContext 给了我一个错误
错误:[未定义]不是组件。的所有子组件必须是 a 或
so I have this app file for react,
const [cart, setCart] = useState([
])
return (
<Router>
<Routes>
<Route element={<Layout />}>
<cartContext.Provider value={{ cart, setCart }}>{/* you wrap provider around the components that you want to have access to the state */}
<Route path="/" index element={<Home/>} />
<Route path="Bracelets" element={<Bracelets />} />
<Route path="Signup" element={<Signup />} />
<Route path="Login" element={<Login />} />
<Route path="Cart" element={<Cart />} />
<Route path="Cart/shipping-buynow" element={<ShippingAddToCart />} />
<Route path="/help" element={<Help />} />
<Route path="/Modal" element={<Modal />} />
<Route path="/Add" element={<Add />} />
{/* <Route path="/create" element={<CreateMeet/>} /> */}
{/* <Route path="/Register" element={<Register />} /> */}
</cartContext.Provider>
</Route>
</Routes>
</Router>
)
}
but for some reason the useContext that I implemented is giving me an error
Error: [undefined] is not a component. All component children of must be a or <React.Fragment>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
Routes 标记
,它会给您一个错误
。在react-router-dom
的version 6
中,您不能在中放置除
标签。route
之外的任何内容...因此,请像下面这样使用
useContext
:它应该可以解决您的问题。
It is giving you an
error
because of theRoutes tag
. Inversion 6
ofreact-router-dom
you can't put anything exceptroute
inside your<Routes>...</Routes>
tag.So use
useContext
like below:It should solve your problem.