Skip to content

Instantly share code, notes, and snippets.

@SmolinPavel
Created April 2, 2019 19:03
Show Gist options
  • Save SmolinPavel/d02fa795b181af8da0ae57198dc20c8f to your computer and use it in GitHub Desktop.
Save SmolinPavel/d02fa795b181af8da0ae57198dc20c8f to your computer and use it in GitHub Desktop.
This is an example when we can't access this.context as the Context component has been upgraded to use the new API while the Legacy component is relying on the old one.
import React, { Component, createContext } from 'react';
import PropTypes from 'prop-types';
const ExampleContext = createContext({ api: 'lorem ipsum' });
/*
* This is an example when we can't access this.context as the Context component
* has been upgraded to use the new API while the Legacy component is relying on the old one.
*/
class Legacy extends Component {
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>
);
}
}
Legacy.propTypes = {
name: PropTypes.string.isRequired
};
Legacy.contextTypes = {
api: PropTypes.string
};
const Context = () => (
<ExampleContext.Provider
value={{ api: 'new api that will be supported in future...' }}>
<Legacy 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