javascript - How to export a context from a class component in Reactjs? - Stack Overflow

I am having problems in export a Context from a Component.The ponent that holds the context is below.i

I am having problems in export a Context from a Component. The ponent that holds the context is below.

import React, { Component, createContext} from 'react';


export const MyContext = React.createContext('');

export default class ComponentOne extends Component {
   ....
   ....
   render(){
     return(
     <MyContext.Provider value={'value1'}>
      <div>....</div>
     </MyContext.Provider>
     )
   }

}


The ponent that wants to import the Context is below

import React, { Component}  from 'react'
import { MyContext } from "../ComponentOne/Index.js"

export default class ComponentTwo extends Component{
render(){
  console.log(this.props)
  return(
    <MyContext.Consumer>
    </MyContext.Consumer>
  )
}
}

The Error I'm getting is

TypeError: render is not a function

I am having problems in export a Context from a Component. The ponent that holds the context is below.

import React, { Component, createContext} from 'react';


export const MyContext = React.createContext('');

export default class ComponentOne extends Component {
   ....
   ....
   render(){
     return(
     <MyContext.Provider value={'value1'}>
      <div>....</div>
     </MyContext.Provider>
     )
   }

}


The ponent that wants to import the Context is below

import React, { Component}  from 'react'
import { MyContext } from "../ComponentOne/Index.js"

export default class ComponentTwo extends Component{
render(){
  console.log(this.props)
  return(
    <MyContext.Consumer>
    </MyContext.Consumer>
  )
}
}

The Error I'm getting is

TypeError: render is not a function

Share Improve this question edited Nov 26, 2019 at 11:07 majid beg asked Nov 26, 2019 at 10:44 majid begmajid beg 1964 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

Context Provider requires a children.

 <MyContext.Provider value={'value1'}>
    <ComponentTwo />
  </MyContext.Provider>

The context is available to MyContext.Provider children.

In your case, you need to render ComponentTwo within the provider, and as a consumer, read the value provided:

import React from 'react';
import ReactDOM from 'react-dom';

export const MyContext = React.createContext('');

class ComponentTwo extends React.Component {
  render() {
    return (
      <MyContext.Consumer>{value => <div>{value}</div>}</MyContext.Consumer>
    );
  }
}

class ComponentOne extends React.Component {
  render() {
    return (
      <MyContext.Provider value={'value1'}>
        <ComponentTwo />
      </MyContext.Provider>
    );
  }
}

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

The error might be because of not reading the value inside the Context.Consumer.

Reference: Context API.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745172253a4614998.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信