简体   繁体   English

在react class组件中调用Context的function

[英]Call function of Context in react class component

I have following Code in my Context.js :我的Context.js中有以下代码:

import { createContext, useEffect, useReducer } from "react";
import Reducer from "./Reducer";

const INITIAL_STATE = {
  user: JSON.parse(localStorage.getItem("user")) || null,
  isFetching: false,
  error: false,
};

export const Context = createContext(INITIAL_STATE);

export const ContextProvider = ({ children }) => {
  const [state, dispatch] = useReducer(Reducer, INITIAL_STATE);

  useEffect(() => {
    localStorage.setItem("user", JSON.stringify(state.user));
  }, [state.user]);

  return (
    <Context.Provider
      value={{
        user: state.user,
        isFetching: state.isFetching,
        error: state.error,
        dispatch,
      }}
    >
      {children}
    </Context.Provider>
  );
};

Now I want to use the Context in a class component and especially call the dispatch function, here a minimal example:现在我想在 class 组件中使用上下文,特别是调用调度 function,这里是一个最小的例子:

export default class Register extends Component {
  static contextType = Context;

  componentDidMount() {
    const { dispatch } = this.context;
    dispatch({ type: "LOGIN_START" });
  }

  render() {
    return (
      <div></div>
    )
  }
}

The Problem now is that dispatch is undefined, when I want to call it inside my class. Before I used the same context with const { dispatch, isFetching } = useContext(Context);现在的问题是dispatch是未定义的,当我想在我的 class 中调用它时。在我使用与const { dispatch, isFetching } = useContext(Context);相同的上下文之前inside a functional component and everything worked fine, what is the problem?在功能组件内部,一切正常,问题是什么?

As suggested in one of the comments above, my ContextProvider has to be an ancestor of my Register component in the component tree.正如上面评论之一所建议的,我的ContextProvider必须是组件树中我的Register组件的祖先。 I simply added it inside my index.js :我只是将它添加到我的index.js中:

...
import { ContextProvider } from "./context/Context";

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <ContextProvider>
    <React.StrictMode>
      <App />
    </React.StrictMode>
  </ContextProvider>
);

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

相关问题 如何使用正确的上下文(this)异步调用父类组件中的函数? - How to asynchronously call function in parent class component with the right context (this)? 如何在 Class 组件的函数内使用 React Context - How to use React Context inside function of Class component “无法将类作为函数调用”在非组件类中进行本地响应 - “Cannot call a class as a function” react native in non component class 如何从基于 class 的组件中的反应 function 调用返回的 const - How to call returned const from a react function in a class based component 如何调用 typescript class 中反应组件内部定义的 function? - How to call a function defined inside react component in a typescript class? 从 React.js 中的另一个 class 组件调用 function - Call a function from another class component in React.js 如果 class 组件中的条件发生反应,如何在内部调用 function? - how to call a function inside if condition in class component react? 反应上下文值在子类组件中不可用 - React context value not available in child class component 反应:在 class 组件中重命名 this.context - React: rename this.context in a class component 如何从全局上下文(一致的UI)调用React组件的函数 - How to call React component's function from a global context (Coherent UI)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM