Skip to content

Instantly share code, notes, and snippets.

@drzhbe
Last active August 8, 2018 04:06
Show Gist options
  • Save drzhbe/621b669469172e2d2d2080e25304ce37 to your computer and use it in GitHub Desktop.
Save drzhbe/621b669469172e2d2d2080e25304ce37 to your computer and use it in GitHub Desktop.
import * as React from 'react';
type Store = { treasure: number };
type Presenter = { digTreasure: (store: Store, place: string) => void };
type MyCompo1Props = { store: Store, presenter: Presenter };
class MyCompo1 extends React.Component<MyCompo1Props> {
render() {
/* should be an error because `this` in `digTreasure` will be not as expected */
return <MyCompo2 onDig={this.digTreasure}/>;
}
digTreasure(place: string) {
const { presenter, store } = this.props;
presenter.digTreasure(store, place);
}
}
type MyCompo2Props = { onDig: (place: string) => void };
class MyCompo2 extends React.Component<MyCompo2Props> {
render() {
return <>
<div onClick={() => this.props.onDig('palm')}>Dig under the palm</div>
<div onClick={() => this.props.onDig('beach')}>Dig on the beach</div>
</>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment