Created
April 1, 2019 21:23
-
-
Save SmolinPavel/cd5f87419aa7ad6d69a2335a2818b335 to your computer and use it in GitHub Desktop.
Example of using React new Context API
This file contains 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, 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