Skip to content

Instantly share code, notes, and snippets.

@atmartins
Last active April 29, 2021 02:14
Show Gist options
  • Save atmartins/649514a67a2bdcc5002d99292755a6ed to your computer and use it in GitHub Desktop.
Save atmartins/649514a67a2bdcc5002d99292755a6ed to your computer and use it in GitHub Desktop.
import React from 'react';
type State = {
isLoading: boolean;
data: object;
};
const initialState = {
isLoading: true,
data: undefined,
}
export const EditProgramMsgs = ({ id }) => {
const [state, setState] = React.useState<State>(initialState);
React.useEffect(() => {
(async () => {
let dataPromise: Promise<DataType>;
if (id) {
const d = await fetch(...);
setState({
isLoading: false,
data: d,
});
}
})();
}, []);
if (state.isLoading) return <div className="section--loading">Loading...</div>;
return (
<>
{ state.data.stuff }
</>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment