面临类型错误:使用 Context API 时渲染不是函数

狄更斯

我是 React 新手,正在学习 Context API,在使用它的过程中,我遇到了这个错误 TypeError: render is not a function。我还找到了这个答案React Context: TypeError: render is not a function in the platform which is close to my problem but no result. 这是我正在使用的代码:

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;
r3wt

您的导出和导入语句有问题。

首先你export class MyContext,那么你立即覆盖MyContextContext.Consumer

修复您的导出语句,然后修复您的导入。导入Context.Consumerin 文件Track,并导入Context.Providerin 文件App

容器/上下文/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};

跟踪.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;

应用程序.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;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用React Context API传递React函数时重新渲染的问题

来自分类Dev

TypeError:使用react-context api时分派不是函数

来自分类Dev

在打字稿中调用rest API时,面临错误,因为“'this'隐式具有'any'类型,因为它没有类型注释”

来自分类Dev

创建新对象时,使用React Context API更新组件

来自分类Dev

使用 React Context API 时,无法获取数据

来自分类Dev

使用React-Context时对象不是可迭代的错误

来自分类Dev

使用Typescript的React的Context API

来自分类Dev

在使用React Context时如何避免意外渲染?

来自分类Dev

运行此代码上下文 API 和 Reactjs 时出现渲染而非函数错误

来自分类Dev

Jqgrid未渲染,并且在调试时显示未定义不是函数错误

来自分类Dev

Jqgrid没有渲染并且在调试时显示未定义不是函数错误

来自分类Dev

使用JVM Nashorn(播放框架)渲染React.js时出错。我得到“空不是函数”

来自分类Dev

仅当或当用户使用React Js Context API登录时才如何获取数据?

来自分类Dev

React Context API不起作用错误:元素类型无效

来自分类Dev

我可以在Context API中使用React Context API还是必须将它们合并?

来自分类Dev

Google Maps API(使用gmapsjs)时,setCenter抛出“未定义不是函数”吗?

来自分类Dev

在Marklogic中使用REST API运行数据中心流时面临的问题

来自分类Dev

使用AngularJS时出现“ .then()不是函数”错误

来自分类Dev

React Context API +钩子

来自分类Dev

使用js.erb文件中的jquery渲染部分错误 渲染不是函数

来自分类Dev

如何在Redux中使用React Context API?

来自分类Dev

在React中使用Context API和功能组件作为服务

来自分类Dev

无法使用Context API React Native检索数据

来自分类Dev

使用Context API传递数据时值未定义

来自分类Dev

React HOC 在 Lifecycle 方法中使用 Context API

来自分类Dev

使用React Context API进行Jest&Enzyme测试错误

来自分类Dev

API端点返回类型错误:“类型”对象不是可迭代

来自分类Dev

调用组件函数时,self.context不是Angular2中的函数

来自分类Dev

使用Akka Java API时Kotlin类型推断编译错误

Related 相关文章

  1. 1

    使用React Context API传递React函数时重新渲染的问题

  2. 2

    TypeError:使用react-context api时分派不是函数

  3. 3

    在打字稿中调用rest API时,面临错误,因为“'this'隐式具有'any'类型,因为它没有类型注释”

  4. 4

    创建新对象时,使用React Context API更新组件

  5. 5

    使用 React Context API 时,无法获取数据

  6. 6

    使用React-Context时对象不是可迭代的错误

  7. 7

    使用Typescript的React的Context API

  8. 8

    在使用React Context时如何避免意外渲染?

  9. 9

    运行此代码上下文 API 和 Reactjs 时出现渲染而非函数错误

  10. 10

    Jqgrid未渲染,并且在调试时显示未定义不是函数错误

  11. 11

    Jqgrid没有渲染并且在调试时显示未定义不是函数错误

  12. 12

    使用JVM Nashorn(播放框架)渲染React.js时出错。我得到“空不是函数”

  13. 13

    仅当或当用户使用React Js Context API登录时才如何获取数据?

  14. 14

    React Context API不起作用错误:元素类型无效

  15. 15

    我可以在Context API中使用React Context API还是必须将它们合并?

  16. 16

    Google Maps API(使用gmapsjs)时,setCenter抛出“未定义不是函数”吗?

  17. 17

    在Marklogic中使用REST API运行数据中心流时面临的问题

  18. 18

    使用AngularJS时出现“ .then()不是函数”错误

  19. 19

    React Context API +钩子

  20. 20

    使用js.erb文件中的jquery渲染部分错误 渲染不是函数

  21. 21

    如何在Redux中使用React Context API?

  22. 22

    在React中使用Context API和功能组件作为服务

  23. 23

    无法使用Context API React Native检索数据

  24. 24

    使用Context API传递数据时值未定义

  25. 25

    React HOC 在 Lifecycle 方法中使用 Context API

  26. 26

    使用React Context API进行Jest&Enzyme测试错误

  27. 27

    API端点返回类型错误:“类型”对象不是可迭代

  28. 28

    调用组件函数时,self.context不是Angular2中的函数

  29. 29

    使用Akka Java API时Kotlin类型推断编译错误

热门标签

归档