Skip to content

Instantly share code, notes, and snippets.

@SmolinPavel
Created April 1, 2019 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SmolinPavel/cd5f87419aa7ad6d69a2335a2818b335 to your computer and use it in GitHub Desktop.
Save SmolinPavel/cd5f87419aa7ad6d69a2335a2818b335 to your computer and use it in GitHub Desktop.
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 (
<div>
<h1>Hello, {name}</h1>
<p>
This example is using{' '}
<span style={{ fontStyle: 'italic', color: 'green' }}>{api}</span>
</p>
</div>
);
}
}
New.propTypes = {
name: PropTypes.string.isRequired
};
const Context = () => (
<ExampleContext.Provider
value={{ api: 'new api that will be supported in future...' }}>
<New name='John Galt' />
</ExampleContext.Provider>
);
export default Context;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment