TypeError: render is not a function updateContextConsumer

TypeError: render is not a function updateContextConsumer

我是 ReactJs 的新手,正在尝试学习 ContextAPI,但我有这个 error.I 阅读有关这种情况的标题,但我无法达到 solution.Firstly 我试图 re-install 反应为旧版本,但它没有 changed.I 尝试将 App 包装在 app.js 而不是 index.js 内,但我得到的结果与现在发生的结果相同。

App.js

import React, { Component } from 'react'
import './App.css';
import UserFinding from './components/UserFinding';

class App extends Component {
render() {
return (
    <div>
        <UserFinding/>
    </div>
      )
  }
}
export default App;

UserFinding.js

import React, { Component } from 'react'
import User from './User.js'
import UserConsumer from '../context.js'

export default class UserFinding extends Component {
render(){
    return(
        <UserConsumer>
            {value=>{
                        const users=value;
                        return(
                            <div>
                                {
                                   users.map(user=>{
                                       return(
                                         <User key={user.id} id={user.id} userName= 
                                         {user.userName} department={user.department}/>
                                       )
                                   }
                                   ) 
                                }
                            </div>
                        )
                     }}
        </UserConsumer>
        ) 
  }
}

User.js

import React, { Component } from 'react'

export default class User extends Component {

state={
    isVisible:true
}
hideShowCard=()=>{
    this.setState({
        isVisible:!this.state.isVisible
    })
}
deleteOnUser=()=>{
    //Dispatch
}
render() {

    return (
            <div>
                <div className="row">
                <div className="col-sm-6">
                    <div className="card">
                        <div className="card-body">
                            <h5 className="card-title">{this.props.userName}<i onClick= 
           {this.deleteOnUser} className="fa fa-trash ml-2"></i></h5>
                            {this.state.isVisible ? <p className="card-text"> 
           {this.props.department}</p>:null}
                            <div onClick={this.hideShowCard} className="btn btn-primary"> 
           {this.state.isVisible ? "Hide" : "Show"}</div>
                        </div>
                    </div>
                </div>
                </div>
            </div>

         )
        }
       }

context.js

import React,{Component} from 'react';
const UserContext=React.createContext();
export class UserProvider extends Component {
 state={
    users:[
      {
        id:1,
        userName:"Ufuk Oral",
        department:"Software Engineering"
      },
      {
        id:2,
        userName:"Emre Çorbacı",
        department:"Data Science"
      }
    ]
   }
 render(){
    return (
        <UserContext.Provider value={this.state}>
            {this.props.children}
        </UserContext.Provider>
    )
 }
}
const UserConsumer=UserContext.Consumer;
export default UserConsumer;

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import UserProvider from './context.js';
ReactDOM.render(
<UserProvider><App/></UserProvider>,document.getElementById('root')); 
serviceWorker.unregister();

这些是我的代码。

您似乎在导出和导入 context.js 文件中的内容有误。 而不是

import UserProvider from './context.js';

尝试这样的事情

import {UserProvider} from './context.js';

你是如何尝试升级反应版本的?你在使用 create-react-app 吗?如果是这样,您还必须更新反应脚本。要升级到所选版本,您必须尝试这样的操作

npm install --save --save-exact react@x.xx react-dom@x.xx 

yarn add --exact react@x.xx react-dom@x.xx

当上下文组件中未添加 'provider' 时,我遇到了这个问题。 这样,

<AuthContext value={allContexts}>
        {children}

我解决了,只是添加了'.Provider' 这样,`

{children} `