Skip to content

Instantly share code, notes, and snippets.



Created Apr 1, 2019
What would you like to do?
Example of using React new Context API
import React, { Component, createContext } from 'react';
import PropTypes from 'prop-types';
const ExampleContext = createContext({ api: 'lorem ipsum' });
class New extends Component {
static contextType = ExampleContext;
render() {
const { name } = this.props;
const { api } = this.context;
return (
<h1>Hello, {name}</h1>
This example is using{' '}
<span style={{ fontStyle: 'italic', color: 'green' }}>{api}</span>
New.propTypes = {
name: PropTypes.string.isRequired
const Context = () => (
value={{ api: 'new api that will be supported in future...' }}>
<New name='John Galt' />
export default Context;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.