Skip to content

Instantly share code, notes, and snippets.

@eamonnboyle
eamonnboyle / App.tsx
Created February 22, 2021 15:22
Weather App Tutorial - Add a Table
function App() {
const [locationSearch, setLocationSearch] = useState('Paris');
const [locations, setLocations] = useState(['Belfast', 'Dublin']);
return (
...
<table>
<thead>
<tr>
<th>Name</th>
@eamonnboyle
eamonnboyle / App.tsx
Created February 22, 2021 15:23
Weather App Tutorial - Repeating Elements
<tbody>
{locations.map(location =>
<tr><td>{location}</td></tr>
)}
</tbody>
@eamonnboyle
eamonnboyle / App.tsx
Created February 22, 2021 15:26
Weather App Tutorial - Add a location
<button onClick={() => setLocations([locationSearch, ...locations])}>Search</button>
@eamonnboyle
eamonnboyle / App.tsx
Created February 22, 2021 15:27
Weather App Tutorial - Breaking out Handlers
const disableSearch = locationSearch.trim() === '';
const addLocation = () => {
setLocations([locationSearch, ...locations]);
setLocationSearch('');
};
return (
...
<button onClick={addLocation} disabled={disableSearch}>Search</button>
@eamonnboyle
eamonnboyle / App.tsx
Created February 22, 2021 15:27
Weather App Tutorial - Tidy Up State
function App() {
const [locationSearch, setLocationSearch] = useState('');
const [locations, setLocations] = useState<string[]>([]);
@eamonnboyle
eamonnboyle / index.tsx
Created February 22, 2021 15:28
Weather App Tutorial - Import Styles in Code
import 'bootstrap/dist/css/bootstrap.min.css';
@eamonnboyle
eamonnboyle / App.tsx
Created February 22, 2021 15:29
Weather App Tutorial - Add Bootstrap Formatting
function App() {
...
return (
<div className="container">
<h1>Weather App</h1>
<div>
<label>
Add Location
<input className="ml-1 mr-1" type="text" value={locationSearch}
onChange={e => setLocationSearch(e.target.value)}/>
@eamonnboyle
eamonnboyle / LocationSearch.tsx
Created February 22, 2021 15:31
Weather App Tutorial - Separating Components - Location Search
import React from "react";
import {FC, useState} from "react";
export const LocationSearch: FC = () => {
return (
<div>
<label>
Add Location
<input className="ml-1 mr-1" type="text" value={locationSearch}
onChange={e => setLocationSearch(e.target.value)}/>
@eamonnboyle
eamonnboyle / LocationSearch.tsx
Last active February 22, 2021 15:32
Weather App Tutorial - Move Search State
export const LocationSearch: FC = () => {
const [locationSearch, setLocationSearch] = useState('');
const disableSearch = locationSearch.trim() === '';
...
@eamonnboyle
eamonnboyle / App.tsx
Created February 22, 2021 15:34
Weather App Tutorial - Use Component with Props
...
<LocationSearch prop1={...} prop2={...}/>
...