【问题标题】:I'm trying to use context api but the value of my Consumer return undefined in the console我正在尝试使用上下文 api,但我的 Consumer 的值在控制台中返回 undefined
【发布时间】:2019-06-24 06:18:44
【问题描述】:

这是我的上下文 api 的代码

import React, { Component } from "react";
const { Provider, Consumer } = React.createContext();
const MyContextProvider = Provider;
const MyContextConsumer = Consumer;
export { MyContextConsumer, MyContextProvider };

class UserState extends Component {
  state = {
    user: "JOHN"
  };

  render() {
    return (
      <MyContextProvider value={this.state}>
        {this.props.children}
      </MyContextProvider>
    );
  }
}

export default UserState;

这是我打算使用它的地方,但它在控制台中返回 undefined。我正在寻求帮助。

import React from "react";
import styled from "@emotion/styled";
import { Formik, Form, Field, ErrorMessage } from "formik";

import { MyContextConsumer } from "../../context/UserStateContext";

const StyledSignUp = styled.div`
  width: 50%;
  margin: 20px auto;
`;

const SignUpForm = ({ props, ...remainProps }) => {
  return (
    <StyledSignUp {...remainProps}>
      <MyContextConsumer>
        {context => {
          console.log(context, "CONTEXT API");
          return <div className='content'>content here</div>;
        }}
      </MyContextConsumer>
    </StyledSignUp>
  );
};

export default SignUpForm;

我想知道我在上下文代码中是否做错了什么。

【问题讨论】:

标签: reactjs 2d-context-api


【解决方案1】:

您在这里遇到的问题是您过早地尝试解构上下文。就我个人而言,我会将其拆分为 3 个文件。当您使用 Git 文件历史记录时,它也会有所帮助!

  • User.context.js(这使得导入更容易跟踪
  • 提供者 - 通常这将是最高节点(确实需要它!)
  • Consumer - 消耗值的组件

User.context.js:

import { createContext } from "react";

export const UserContext = createContext();

提供者 - &lt;UserState /&gt;:

import React, { Component } from "react";

import { UserContext } from '../../User.context.js' // some folder for context's

class UserState extends Component {
  state = {
    user: "JOHN"
  };

  render() {
    return (
      <UserContext.Provider value={this.state}>
        {this.props.children}
      </UserContext.Provider>
    );
  }
}

export default UserState;

消费者 - &lt;SignUpForm /&gt;:

import React, { useContext } from "react";
import styled from "@emotion/styled";

import { UserContext } from '../../User.context.js' // some folder for context's

const StyledSignUp = styled.div`
  width: 50%;
  margin: 20px auto;
`;

// OLD WAY
const SignUpForm = ({ props, ...remainProps }) => {
  return (
    <UserContext.Consumer>
      {context => {
        console.log("CONTEXT API", context);

        return (
          <StyledSignUp {...remainProps}>
            <div className="content">content here</div>;
          </StyledSignUp>
        );
      }}
    </UserContext.Consumer>
  );
};

// React Hook way :)
const SignUpForm = ({ props, ...remainProps }) => {
  const context = useContext(UserContext);

  return (
    <StyledSignUp {...remainProps}>
      <div className="content">content here</div>;

      {/* Debug - Seeing is believing */}
      <pre>{JSON.stringify(context, null, 2)}</pre>
    </StyledSignUp>
  );
};

export default SignUpForm;

让我知道你过得怎么样!

【讨论】:

  • 我喜欢你所做的反应钩子方式。我昨天能够调试它。原因是我忘记将它从最高组件(即我的 layout.js 中包含页眉和页脚)向下传递。但实际上,我刚刚从你做那种反应钩子的方式中学到了一些新东西。从现在开始,我一定会和它一起玩。非常感谢你帮助我解决这个问题。你有推特或我可以关注的东西吗? @尼尔
  • 感谢您的客气话,我会查看 dan_abramov 和 kentcdodds 等 Twitter 帐户。如果您可以为答案投票,如果您发现它有帮助,那就太好了。
猜你喜欢
  • 1970-01-01
  • 2021-02-09
  • 1970-01-01
  • 1970-01-01
  • 2021-02-10
  • 1970-01-01
  • 2023-03-28
  • 2021-05-31
  • 1970-01-01
相关资源
最近更新 更多