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
| // This function takes a component... | |
| export function withAppContext(Component) { | |
| // ...and returns another component... | |
| return function ComponentBoundWithAppContext(props) { | |
| // ... and renders the wrapped component with the current context! | |
| // Notice that we pass through any additional props as well | |
| return ( | |
| <AppContextConsumer> | |
| {appContext => <Component {...props} appContext={appContext} />} | |
| </AppContextConsumer> |
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 * as React from "react"; | |
| import { AppContextInterface, withAppContext } from "./AppContext"; | |
| const PostInfoComp = ({ appContext }: { appContext?: AppContextInterface }) => | |
| appContext && ( | |
| <div> | |
| Name: {appContext.name} <br /> | |
| Author: {appContext.author} <br /> | |
| Url: {appContext.url} | |
| </div> |
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
| type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>; | |
| export function withAppContext< | |
| P extends { appContext?: AppContextInterface }, | |
| R = Omit<P, 'appContext'> | |
| >( | |
| Component: React.ComponentClass<P> | React.StatelessComponent<P> | |
| ): React.SFC<R> { | |
| return function BoundComponent(props: R) { | |
| return ( |
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
| <AppContextConsumer> | |
| {appContext => ...} | |
| </AppContextConsumer> |
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 * as React from 'react'; | |
| import { AppContextConsumer } from './AppContext'; | |
| export const PostInfo = () => ( | |
| <AppContextConsumer> | |
| {appContext => appContext && ( | |
| <div> | |
| Name: {appContext.name}, | |
| Author: {appContext.author}, | |
| Url: {appContext.url} |
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 * as React from 'react'; | |
| import { AppContextInterface, AppContextProvider } from './AppContext'; | |
| const sampleAppContext: AppContextInterface = { | |
| name: 'Using React Context in a Typescript App', | |
| author: 'thehappybug', | |
| url: 'http://www.example.com' | |
| }; | |
| export const App = () => ( |
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 * as React from 'react'; | |
| export interface AppContextInterface { | |
| name: string, | |
| author: string, | |
| url: string | |
| } | |
| const ctxt = React.createContext<AppContextInterface | null>(null); |
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 * as React from 'react'; | |
| interface AppContextInterface { | |
| name: string, | |
| author: string, | |
| url: string | |
| } | |
| const {Provider, Consumer} = React.createContext<AppContextInterface | null>(null); |
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 * as React from 'react'; | |
| interface AppContextInterface { | |
| name: string, | |
| author: string, | |
| url: string | |
| } | |
| const {Provider, Consumer} = React.createContext<AppContextInterface>(); |
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
| npm update react react-dom @types/react @types/react-dom |