简体   繁体   English

组合 useContext 提供者

[英]Combining useContext Providers

as im creating a reactjs project, i have devided my context into three separate files, therefore i ended up with three context providers, at first i wrapped all the context providers one by one around my components in app.js, but i figures maybe if i combined all the context providers in one component and use this component to wrap my app.js so i created the file with below code当我创建一个 reactjs 项目时,我将我的上下文分为三个单独的文件,因此我最终得到了三个上下文提供程序,起初我将所有上下文提供程序一个一个地包装在我的 app.js 组件周围,但我想也许如果我将所有上下文提供程序组合在一个组件中并使用此组件来包装我的 app.js 所以我使用以下代码创建了文件

import React from "react";
import CryptoContextProvider from "./crypto-context";
import NewsContextProvider from "./news-context";
import DataContextProvider from "./index-context";

const allContextProviders = (props) => {
  return (
    <>
      <CryptoContextProvider>
        <NewsContextProvider>
          <DataContextProvider>{props.children}</DataContextProvider>
        </NewsContextProvider>
      </CryptoContextProvider>
    </>
  );
};

export default allContextProviders;

then i wrrap my app.js as follows然后我按如下方式包装我的 app.js

import allContextProviders from "./store/allContextProviders";

function App() {
  return (
    <allContextProviders>
      <div className="App">
        <Navbar />
        <Routes>
          <Route path="/" element={<Navigate to="/welcome" />} />
          <Route path="/welcome" element={<Home />} />
          <Route path="/marketplace" element={<Marketplace />} />
          <Route path="/tools" element={<ToolsServices />} />

yet when i trie to wrap my app.js with the allContextProviders component i get this following error然而,当我尝试用 allContextProviders 组件包装我的 app.js 时,出现以下错误

Line 12:8:  'allContextProviders' is defined but never used  no-unused-vars

am i doing something wrong here?我在这里做错了什么吗? your feedback is appreciated感谢您的反馈

In React to tell it that the variable name is Component ( and please evaluate it ), the name of the component should start with a capital letter like AllContextProvider.在 React 中告诉它变量名是 Component(并请评估它),组件的名称应该以大写字母开头,如 AllContextProvider。 Please change it's name so that React knows and it will evaluate it in run time.请更改它的名称,以便 React 知道并在运行时对其进行评估。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM