Created
April 18, 2020 16:44
-
-
Save suhas86/835668b490f97c59cd4b47b6ec2d63cb to your computer and use it in GitHub Desktop.
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, createContext } from 'react' | |
type teamState = { | |
team:string | |
} | |
export const TeamContext = createContext<{team:string,changeTeam:any}>({team:"rcb",changeTeam:null}) | |
class TeamProvider extends Component<{},teamState> { | |
constructor(props:{}){ | |
super(props); | |
this.changeTeam = this.changeTeam.bind(this) | |
} | |
state: teamState = { | |
team:"rcb" | |
} | |
changeTeam(e:React.ChangeEvent<HTMLInputElement>) { | |
this.setState({team:e.target.value}) | |
} | |
render() { | |
return ( | |
<TeamContext.Provider value={{...this.state,changeTeam:this.changeTeam}}> | |
{this.props.children} | |
</TeamContext.Provider> | |
) | |
} | |
} | |
export default TeamProvider; | |
/** Higher order component */ | |
export const withTeam = (Component:any) => (props:any) => ( | |
<TeamContext.Consumer> | |
{(value) => <Component teamContext={value} {...props} />} | |
</TeamContext.Consumer> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment