React context consumer was rendered with multiple children 错误

React context consumer was rendered with multiple children error

我只是尝试使用 React Provider 但不知何故我得到了这个错误: index.js:1437 警告:使用多个子项或不是函数的子项呈现上下文使用者。上下文使用者期望一个作为函数的子项。如果您确实传递了一个函数,请确保它周围没有尾随或前导空格。

这是context.js

import React, { Component } from 'react'
    const Context=React.createContext()
    class Providerr extends Component {
    state={display:false}
    displayeditor=()=>{this.setState({display:!this.state.display})  }
    render() { return (
      <Context.Provider value={{...this.state,displayeditor:this.displayeditor  }}>
          {this.props.children}
      </Context.Provider>
    )
  }
}
    const Consumer=Context.Consumer
export {Providerr,Consumer}

我要在下方消费

import React, { Component } from 'react'
import {NavLink} from "react-router-dom"
import "./leagues.scss"
import {Consumer} from "./context.js"
export default class Navbar extends Component {

  render() {
    return (
        <nav className="navbar navbar-expand-lg">
           <a className="navbar-brand" href="#Home">
             <img src="./images/customLogo.jpg" className="navlogo"/>
           </a>        
           <div className="collapse navbar-collapse">
             <ul className="navbar-nav mx-5"> 
             <NavLink to="/">
             <li className="nav-item active mr-5">
               <div className="nav-link">
                 <span>Home</span>
               </div>  
             </li>
             </NavLink>
             <Consumer> {value =>      
             <li onClick={value.displayeditor}  className="nav-item mr-5">
               <div className="nav-link modal_lig" href=""> 
                 <span>Leagues</span>    
               </div>  
             </li> }
            </Consumer>
         </nav>     
 )}}

将整个代码包裹在 Consumer 中,如下所示。另请注意,您的某些 html 元素未正确关闭。

    import React, { Component } from 'react'
import {NavLink} from "react-router-dom"
import "./leagues.scss"
import {Consumer} from "./context.js"
export default class Navbar extends Component {

    render(){
        return(
            <Consumer> 
                {value => (
                    <nav className="navbar navbar-expand-lg">
                        <a className="navbar-brand" href="#Home">
                            <img src="./images/customLogo.jpg" className="navlogo"/>
                        </a>        
                        <div className="collapse navbar-collapse">
                            <ul className="navbar-nav mx-5"> 
                                <NavLink to="/">
                                    <li className="nav-item active mr-5">
                                        <div className="nav-link">
                                            <span>Home</span>
                                        </div>  
                                    </li>
                                </NavLink>
                                <li onClick={()=>value.displayeditor()}  className="nav-item mr-5">
                                    <div className="nav-link modal_lig" href=""> 
                                        <span>Leagues</span>    
                                    </div>  
                                </li>
                            </ul>
                        </div>
                    </nav> 
                )} 
            </Consumer>
        )
    }
}