Created
July 1, 2018 16:20
-
-
Save ShMcK/c82b0e0e07090cb2ae5312d79f8d5071 to your computer and use it in GitHub Desktop.
VisualizingState: State !== Data
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
type States = { | |
mode: 'loading' | 'loaded' | 'error', | |
data: any, | |
} | |
class DataLoader extends React.Component<{}, States> { | |
state = { | |
mode: 'loading', // state | |
data: {}, // data | |
} | |
componentDidMount() { | |
this.props.load() | |
.then(({ data, error }) => { | |
if (!error) { | |
this.setState({ mode: 'loaded', data }) | |
} else { | |
this.setState({ mode: 'error', data: error }) | |
} | |
}) | |
} | |
render() { | |
const { mode, data } = this.state | |
switch(mode) { | |
case 'loading': | |
return <Loading /> | |
case 'loaded': | |
return <Loaded data={data} /> | |
default: | |
return <Error data={data} /> | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment