简体   繁体   English

面临类型错误:使用上下文 API 时,渲染不是 function

[英]Faced TypeError: render is not a function when using Context API

I am new to React ans was learning Context API and during the use of it I faced this error TypeError: render is not a function.我是 React 的新手,正在学习 Context API,在使用它的过程中我遇到了这个错误 TypeError: render is not a function。 I also found the this answer React Context: TypeError: render is not a function in the platform which is close to my problem but no result.我还找到了这个答案React Context: TypeError: render is not a function in the platform 这与我的问题很接近,但没有结果。 Here is the code I am using:这是我正在使用的代码:

import React, { Component } from "react";
import MyContext from "../../Containers/Context/Context";
class Track extends Component {
  render() {
    return (
      <MyContext>
        {value => {
          return <div>{value.heading}</div>;
        }}
      </MyContext>
    );
  }
}

export default Track;

import React, { Component } from "react";

const Context = React.createContext();

export class MyContext extends Component {
  state = { track_list: [], heading: "Top Ten Tracks" };
  render() {
    return (
      <Context.Provider value={this.state}>
        {this.props.children}
      </Context.Provider>
    );
  }
}

export default MyContext = Context.Consumer;

import React, { Component, Fragment } from "react";
import "./App.css";
import Header from "../src/Components/Header/Header";
import Search from "../src/Components/Search/Search";
import Tracks from "../src/Components/Tracks/Tracks";
import { BrowserRouter as Router, Route, Link, Switch } from "react-router-dom";
import NotFound from "./Components/NotFound/NotFound";
import MyContext from "./Containers/Context/Context";

class App extends Component {
  render() {
    return (
      <MyContext>
        <Router>
          <Fragment>
            <Header />
            <div className="container">
              <Search />
              <Switch>
                <Route exact path="/" component={Tracks} />
                <Route component={NotFound} />
              </Switch>
            </div>
          </Fragment>
        </Router>
      </MyContext>
    );
  }
}

export default App;

Your export and import statements are problematic.您的导出和导入语句有问题。

first you export class MyContext then you immediately overwrite MyContext with Context.Consumer .首先你export class MyContext然后你立即用Context.Consumer覆盖MyContext

Fix your export statements and then fix your imports.修复您的导出语句,然后修复您的导入。 import the Context.Consumer in file Track , and import the Context.Provider in file App在文件Track中导入Context.Consumer ,并在文件App中导入Context.Provider

Containers/Context/Context.js容器/上下文/Context.js

import React, { Component } from "react";

const Context = React.createContext();

class MyContextProvider extends Component {
  state = { track_list: [], heading: "Top Ten Tracks" };
  render() {
    return (
      <Context.Provider value={this.state}>
        {this.props.children}
      </Context.Provider>
    );
  }
}

const MyContextConsumer = Context.Consumer;

export {MyContextProvider,MyContextConsumer};

Track.js Track.js

import React, { Component } from "react";
import {MyContextConsumer} from "../../Containers/Context/Context";
class Track extends Component {
  render() {
    return (
      <MyContextConsumer>
        {value => {
          return <div>{value.heading}</div>;
        }}
      </MyContextConsumer>
    );
  }
}

export default Track;

App.js应用程序.js

import React, { Component, Fragment } from "react";
import "./App.css";
import Header from "../src/Components/Header/Header";
import Search from "../src/Components/Search/Search";
import Tracks from "../src/Components/Tracks/Tracks";
import { BrowserRouter as Router, Route, Link, Switch } from "react-router-dom";
import NotFound from "./Components/NotFound/NotFound";
import {MyContextProvider} from "./Containers/Context/Context";

class App extends Component {
  render() {
    return (
      <MyContextProvider>
        <Router>
          <Fragment>
            <Header />
            <div className="container">
              <Search />
              <Switch>
                <Route exact path="/" component={Tracks} />
                <Route component={NotFound} />
              </Switch>
            </div>
          </Fragment>
        </Router>
      </MyContextProvider>
    );
  }
}

export default App;

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

相关问题 TypeError: 渲染不是 function 上下文 Api 多个上下文 - TypeError: render is not a function Context Api Multiple Context TypeError: dispatch is not a function when using react-context api - TypeError: dispatch is not a function when using react-context api React Context:TypeError:render不是函数 - React Context: TypeError: render is not a function 在反应中使用上下文但它们没有加载。 我收到 TypeError: render is not a function - Using context in react but they are not loading. I get TypeError: render is not a function 尝试使用 Context API 时看到“render is not a function” - Seeing "render is not a function" when trying to use Context API React TypeError: Object(...)(...) is undefined when using Context API - React TypeError: Object(...)(...) is undefined when using Context API React 测试库 - 使用渲染时 TypeError trim 不是 function - React Testing Library - TypeError trim is not a function when using render 运行此代码上下文API和Reactjs时出现Render not Function错误 - Getting a Render not Function Error, when I run this Code Context API and Reactjs 使用&#39;flickrnode&#39;API时出现&#39;TypeError:undefined is not function&#39;错误 - 'TypeError: undefined is not a function' error when using 'flickrnode' API TypeError: (intermediate value).map is not a function when using set for Country Api - TypeError: (intermediate value).map is not a function when using set for Country Api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM