Skip to content

Instantly share code, notes, and snippets.

@suhas86
Created April 18, 2020 16:44
Show Gist options
  • Save suhas86/835668b490f97c59cd4b47b6ec2d63cb to your computer and use it in GitHub Desktop.
Save suhas86/835668b490f97c59cd4b47b6ec2d63cb to your computer and use it in GitHub Desktop.
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