简体   繁体   English

使用带有上下文的全局状态

[英]Use a global state with Context

I'm currently trying to set a globalstate into my app.我目前正在尝试在我的应用程序中设置一个全局状态。 It's just about counting points, so a global "score".这只是计算点数,因此是全球“分数”。 Context seemed easy and I followed some tutorials, but it keeps sending errors.上下文似乎很简单,我遵循了一些教程,但它不断发送错误。

In my app, I am using React and BrowserRouter to switch between pages.在我的应用程序中,我使用 React 和 BrowserRouter 在页面之间切换。

App.jsx You can see, I tried to wrap with the Store Component App.jsx可以看到,我尝试用 Store 组件包裹

export default function App() {
  return (
    <main>
      <Routes>
        <Store>
          <Route path="/" element={<Welcome />} />
          <Route path="/whoareyou" element={<WhoAreYou />} />
          <Route path="/questionone" element={<QuestionOne />} />
          <Route path="/questiontwo" element={<QuestionTwo />} />
          <Route path="/questionthree" element={<QuestionThree />} />
          <Route path="/result" element={<Result />} />
        </Store>
      </Routes>
    </main>
  );
}

Points.js This is my Context file Points.js这是我的上下文文件

import React, { useState, createContext } from "react";

const initialState = { points: 0 };

export const Context = createContext(initialState);

export const Store = ({ children }) => {
  const [state, setState] = useState(initialState);
  return (
    <Context.Provider value={[state, setState]}>{children}</Context.Provider>
  );
};

Component Here is my component, where I want to count a 1 into the "points" Score组件这是我的组件,我想将 1 计入“分数”分数

export default function WhoAreYou() {
  const [state, setState] = useContext(Context);

  return (
    <StyledSection variant="center">
      <StyledNavLink to="/questionone">
        <StyledButton
          type="button"
          variant="next-button"
          onClick={() => {
            console.log(state.points);
          }}
        >
          NEXT
        </StyledButton>
      </StyledNavLink>
    </StyledSection>
  );
}

Errors错误

This are the error messages:这是错误消息:

undefined is not an object (evaluating 'element.type.name') undefined 不是一个对象(评估'element.type.name')

Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined.警告:React.jsx:类型无效——需要一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:未定义。 You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。

Check the render method of App .检查App的渲染方法。 App Router BrowserRouter应用路由器浏览器路由器


To See the whole code, you can see this CodeSandBox:要查看整个代码,您可以查看此 CodeSandBox:

https://codesandbox.io/s/empty-https-gpkq0l?file=/src/components/pages/WhoAreYou.jsx:581-1422 https://codesandbox.io/s/empty-https-gpkq0l?file=/src/components/pages/WhoAreYou.jsx:581-1422


I would be very thankful if you guys could help!!如果你们能提供帮助,我将非常感谢!

导入存储为命名导入(您将其导出为命名导出)

import { Store } from "../src/components/context/Points";

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

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