Skip to content

Instantly share code, notes, and snippets.

@eamonnboyle
eamonnboyle / LocationSearch.tsx
Created February 22, 2021 15:35
Weather App Tutorial - Specify Component Props
interface LocationSearchProps {
onSearch: (search: string) => void;
}
export const LocationSearch: FC<LocationSearchProps> = (props) => {
...
@eamonnboyle
eamonnboyle / LocationSearch.tsx
Last active February 22, 2021 15:37
Weather App Tutorial - Destructuring Props
export const LocationSearch: FC<LocationSearchProps> = (props) => {}
// Simplify using destructuring
export const LocationSearch: FC<LocationSearchProps> = ({onSearch}) => {}
@eamonnboyle
eamonnboyle / LocationSearch.tsx
Created February 22, 2021 15:40
Weather App Tutorial - Invoke Callback
export const LocationSearch: FC<LocationSearchProps> = ({onSearch}) => {
const [locationSearch, setLocationSearch] = useState('');
const disableSearch = locationSearch.trim() === '';
const addLocation = () => {
onSearch(locationSearch); // Prop callback invoked - string passed
setLocationSearch('');
};
return (
@eamonnboyle
eamonnboyle / index.tsx
Created February 22, 2021 15:41
Weather App Tutorial - Using Component in App
...
import {LocationSearch} from "./LocationSearch";
function App() {
const [locations, setLocations] = useState<string[]>([]);
const addLocation = (location: string) => setLocations([location, ...locations]);
return (
<div className="container">
<h1>Weather App</h1>
@eamonnboyle
eamonnboyle / App.tsx
Created February 22, 2021 15:44
Weather App Tutorial - Using Table Component in App
...
import {LocationSearch} from "./LocationSearch";
import {LocationTable} from "./LocationTable";
function App() {
...
return (
<div className="container">
<h1>Weather App</h1>
<LocationSearch onSearch={addLocation}/>
@eamonnboyle
eamonnboyle / WeatherService.ts
Last active February 23, 2021 10:22
Weather App Tutorial - Reading Configuration
const key: string = process.env.REACT_APP_OPEN_WEATHER_API_KEY as string;
if (key === undefined) {
throw new Error('No Open Weather API Key defined - ensure you set a variable called REACT_APP_OPEN_WEATHER_API_KEY')
}
const keyQuery = `appid=${key}`
const server = 'http://api.openweathermap.org/data/2.5';
@eamonnboyle
eamonnboyle / WeatherService.ts
Created February 23, 2021 10:23
Weather App Tutorial - Template Literals
`${server}/weather?q=${term}&${keyQuery}`
@eamonnboyle
eamonnboyle / Weather.ts
Created February 23, 2021 10:23
Weather App Tutorial - Weather Model
export interface Coordinates {
lon: number;
lat: number;
}
export interface WeatherLocation {
coord: Coordinates;
id: number;
name: string;
}
@eamonnboyle
eamonnboyle / WeatherService.ts
Created February 23, 2021 10:24
Weather App Tutorial - Search Service
export async function searchLocation(term: string): Promise<WeatherLocation | undefined> {
const result = await fetch(`${server}/weather?q=${term}&${keyQuery}`);
if (result.status === 404) return undefined;
if (result.status !== 200) throw new Error('Failed to read location data');
return await result.json();
}
@eamonnboyle
eamonnboyle / App.tsx
Created February 23, 2021 10:26
Weather App Tutorial - State Updates
...
const App: FC = () => {
const [locations, setLocations] = useState<WeatherLocation[]>([]);
const [error, setError] = useState('');
const [warning, setWarning] = useState('');
...