简体   繁体   English

上下文 API 问题 - object 未定义

[英]Context API problem - object is undefined

I've been always using Redux, but I finally decided to try to play with Context API, but apparently I'm missing something.我一直在使用 Redux,但我最终决定尝试使用 Context API,但显然我错过了一些东西。 I get an error "TypeError: Object(...)(...) is undefined" in App.js, weather and details appear to be undefined.我在 App.js 中收到错误“TypeError: Object(...)(...) is undefined”,天气和详细信息似乎未定义。

Could you please check out my code in order to help me out in finding a mistake?您能否查看我的代码以帮助我找出错误? I thought I should have a direct access to the state without the need of destructuring.我想我应该可以直接访问 state 而无需解构。 Or maybe destructuring is the only way to go?或者也许解构是 go 的唯一方法?

Here you can see all parts of my code - context + wrapped app.js.在这里,您可以看到我的代码的所有部分 - 上下文 + 包装的 app.js。

MyContext.js MyContext.js

const WeatherContext = React.createContext();

class WeatherProvider extends Component {
  state = {
    query: "",
    weather: {},
    details: {},
    mean: "",
    mode: "",
  };

  //my functions here

  render() {
    return (
      <WeatherContext.Provider
        value={{
          ...this.state,
          search: this.search,
          meanValue: this.meanValue,
          ModeValue: this.modeValue,
        }}
      >
        {this.props.children}
      </WeatherContext.Provider>
    );
  }
}

const WeatherConsumer = WeatherContext.Consumer;

export { WeatherConsumer, WeatherContext };

export default WeatherProvider;

App.js应用程序.js

import React, { useContext } from "react";

import WeatherContext from "./context/MyContext";

import Cards from "./components/Cards";
import Header from "./components/Header";
import CurrentWeather from "./components/CurrentWeather";
import Footer from "./components/Footer";
import Summary from "./components/Summary";

const App = () => {
  const { weather, details } = useContext(WeatherContext);

  return (
    <div
      className={
        typeof details.current != "undefined"
          ? weather.list[0].main.temp > 15
            ? "app warm"
            : "app cold"
          : "app"
      }
    >
      <main>
        <Header />
        {typeof details.current != "undefined" && (
          <>
            <CurrentWeather />
            <Cards />
            <Summary />
            <Footer />
          </>
        )}
      </main>
    </div>
  );
}

export default App;

Everything is wrapped in index.js.一切都包含在 index.js 中。

Index.js索引.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import WeatherProvider from "./context/MyContext"



ReactDOM.render(
  <WeatherProvider>
    <App />
  </WeatherProvider>,
  document.getElementById('root')
);

serviceWorker.unregister();

I think you are not importing your context correctly at:我认为您没有正确导入上下文:

import WeatherContext from "./context/MyContext";

You exported as named in its module, so you need to import it like below:您在其模块中以命名方式导出,因此您需要像下面这样导入它:

import { WeatherContext } from "./context/MyContext";

at app.js fileapp.js文件中

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

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