Created
April 1, 2019 20:53
-
-
Save SmolinPavel/3e9be8e0b4b73444d4459708e02ae6e5 to your computer and use it in GitHub Desktop.
Example of using the React legacy Context API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import PropTypes from 'prop-types'; | |
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 | |
}; | |
class Context extends Component { | |
getChildContext() { | |
return { | |
api: 'legacy api that will not be supported in future...' | |
}; | |
} | |
render() { | |
return <Legacy name='John Galt' />; | |
} | |
} | |
Context.childContextTypes = { | |
api: PropTypes.string | |
}; | |
export default Context; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment