-
-
Save nonsensecreativity/f526d5e76aaa1caf31b103042fef1a7f to your computer and use it in GitHub Desktop.
React Higher Order Components in TypeScript
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 * as React from 'react'; | |
import { Component } from 'react'; | |
export default function HOCBaseRender<Props, State, ComponentState>( | |
Comp: new() => Component<Props & State, ComponentState>) { | |
return class HOCBase extends Component<Props, State> { | |
render() { | |
return <Comp {...this.props} {...this.state}/>; | |
} | |
} | |
} |
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 * as React from 'react'; | |
import { Component } from 'react'; | |
import HOCBaseRender from './HOCBaseRender'; | |
export default function HOCMounted<Props, ComponentState>( | |
Comp: new() => Component<Props, ComponentState>, onMount: () => void, onUnmount: () => void) { | |
return class HOCWrapper extends HOCBaseRender<Props, void, ComponentState>(Comp) { | |
// ... Implementation | |
componentWillMount() { | |
onMount.call(this); | |
} | |
componentWillUnmount() { | |
onUnmount.call(this); | |
} | |
} | |
} |
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 * as React from 'react'; | |
import NameAndAge from './NameAndAge'; | |
import HOCStateToProps from './HOCStateToProps'; | |
export default HOCStateToProps< | |
{ name: string }, | |
{ age: number }, | |
void>(NameAndAge, () => ({ age: 12 })); |
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 * as React from 'react'; | |
import { Component } from 'react'; | |
import HOCBaseRender from './HOCBaseRender'; | |
export default function HOCStateToProps<Props, State, ComponentState>( | |
Comp: new() => Component<Props & State, ComponentState>, getState: () => State) { | |
return class HOCWrapper extends HOCBaseRender<Props, State, ComponentState>(Comp) { | |
// ... Implementation | |
constructor() { | |
super(); | |
this.state = getState(); | |
} | |
} | |
} |
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 * as React from 'react'; | |
import { Component } from 'react'; | |
export default class NameAndAge extends Component<{ name: string, age: number }, void> { | |
render() { | |
return <div>Name: {this.props.name}, Age: {this.props.age}</div>; | |
} | |
} |
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 * as React from 'react'; | |
import * as ReactDOM from 'react-dom'; | |
import HOCNameAndAge from './HOCNameAndAge'; | |
ReactDOM.render(<HOCNameAndAge name='Hello'/>, document.getElementById('root')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment