Why Context value showing undefined?
src/Context.js:
import React, { Component } from 'react';
const Context = React.createContext();
export class Provider extends Component {
state = { a: 1, b: 2 };
render() {
return (
<Context.Provider value={this.state}>
{this.props.children}
</Context.Provider>
);
}
}
export const Consumer = Context.Consumer;
src/country/CountryList.js:
import React, { Component } from 'react';
import { Consumer } from '../../Context';
class CountryList extends Component {
render() {
return (
<Consumer>
{value => {
console.log('val:' + value);
}}
</Consumer>
);
}
}
export default CountryList;
Trying to pass context value in CountryList but it's showing undefined, can't figure out why. Thanks in Advance