Skip to content

Commit

Permalink
feat: create <Resolve> component
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jan 21, 2018
1 parent 9283a1e commit c682a9f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/mock.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Create a mock and implement it
const Player = mock();

// Now you can already use <Player>.
<Player />

// But implement it only later.
Player.implement(RealPlayer);
Expand Down
54 changes: 54 additions & 0 deletions src/Resolve/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {Component} from 'react';

export interface IResolveProps {
promise: Promise<any>;
children?: (state: IResolveState) => React.ReactElement<any>;
}

export interface IResolveState {
pending: boolean;
value?: any;
error?: any;
}

export class Resolve extends Component<IResolveProps, any> {
mounted = false;

state: IResolveState = {
pending: true
};

constructor (props, context) {
super(props, context);

this.props.promise
.then((value) => {
if (this.mounted) {
this.setState({
pending: false,
value
});
}
})
.catch((error) => {
if (this.mounted) {
this.setState({
pending: false,
error
});
}
})
}

componentDidMount () {
this.mounted = true;
}

componentWillUnmount () {
this.mounted = false;
}

render () {
return this.props.children(this.state);
}
}

0 comments on commit c682a9f

Please sign in to comment.