Skip to content

Instantly share code, notes, and snippets.

@nikgraf
Last active November 20, 2018 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikgraf/1165cc9f1b1ba1cef9e577e636c7745b to your computer and use it in GitHub Desktop.
Save nikgraf/1165cc9f1b1ba1cef9e577e636c7745b to your computer and use it in GitHub Desktop.
React Exercises

Setup

npx create-react-app my-app
cd my-app

Change the version of react and react-dom in package.json to 16.7.0-alpha.2. Run npm install and then npm run start

Remove all files from the src/ directory and create a new index.js with the following content:

import React from "react";
import ReactDOM from "react-dom";

function App() {
  return <h1>Places</h1>;
}

ReactDOM.render(<App />, document.getElementById("root"));

Note If you are done early at any step setup Jest & react-testing-library and create tests for each of the components.

React - Rendering & Components

  1. Render the whole list into one component. It should contain a preview only showing the name.
  2. Add conditinal divs rendering the phone and email address if available in the data.
  3. Add a Phone component that replaces +43 with 0 and replace the phone rendering using this component
  4. Add the following styles to your application and create a generic Card component rendering a div that's used to wrap each place entry in the list.

Hint leverage children to avoid that Card needs to know anything about place props

ul {
  list-style-type: none;
  padding: 0;
}
.card {
  display: block;
  border: 1px solid #ccc;
  margin-bottom: 1rem;
  padding: 1rem;
}
  1. Bonus Exercise: Convert the card to accept a tag prop so we can make the card an li-element instead of having a div inside and li.

React - Interaction & State Management

You can start from your own app or use this template https://www.dropbox.com/s/gdrfd8ombtkw28i/my-app-1.zip?dl=0

  1. Create an AddPlace form with 3 fields: name, phone, email and a submit button. Place the form below the list of places.
  2. Move the places data into the state of the App and setup the AddPlace form to create a new entry.
  3. Make sure the namev is a required field and the form is reset everytime you click submit.

Hint This is tedious work, and for real world use-cases eventually you want to use form library like Formik or final-form.

React - Refs & Effects

  1. Use useRef & useEffect to build a Map component. Use the Google Maps API, Leaflet or similar.
  2. For each entry show a small map and center the map at the location position.
  3. Bonus Exercise: Add one larger map on top that accepts a prop markers incl. the position and name. Use this to display all the places with markers.

React - Styling

  1. Refactor the Card styling to use inline styles, css modules, styled-components or emotion.
  2. Pick a styling solution of your choice.
  3. Improve the list layout to on the left side show the banner image of the location. If no image is available show the map. On the right side the details like name, phone, email and so on.

Feedback Form

https://docs.google.com/forms/d/e/1FAIpQLSduti27Gold0T3WDtSe04Hi3V-yaBG0ChqAbS-t3-qoe4qsbA/viewform

export default {
places: [
{
id: "2ea45f13-3a69-4583-a789-c92b25405760",
name: "FlipLab Vienna",
description:
"Come in and Flip out! Mit FLIP LAB ... expanding my limits setzen wir neue Maßstäbe in der Welt des Trampolinsports, Freestyle- und Parkour-Runnings. Auf mehr als 2.500 Quadratmetern findest du eine Erlebniswelt, die dich stundenlang beschäftigen kann. Für jeden ist hier etwas dabei! Fun-Trampoline, Dodgeball-Fields, Wandtrampoline, Basketball- und Freestyltrampoline mit Bounceboards, Bungees und Halfpipe sowie Parkour oder auch eine Ninja Warrior Area sind im FlipLab Vienna anzutreffen.",
location: {
lat: 48.14099,
long: 16.48131
},
email: "info@fliplab.at",
phone: "+43 1 2266033",
images: [
{
url:
"https://files.eversports.com/37c249f4-fa82-40c2-a4c0-d734a3890384/fliplabfalse_at_70125_eversports_3jpg-x-large.jpg",
isBanner: true
},
{
url:
"https://files.eversports.com/8df7dca0-e8f3-4e0d-b021-846657c6d653/fliplabfalse_at_70125_eversports_1jpg-x-large.jpg",
isBanner: false
},
{
url:
"https://files.eversports.com/99975538-394f-4a86-8818-d6754a4f7b83/fliplabfalse_at_70125_eversports_2jpg-x-large.jpg",
isBanner: false
}
]
},
{
id: "c7003667-6fd5-47d9-83be-ac2f367a627b",
name: "Sport König",
description:
"Bei Sport König im Industriegebiet Hagenbrunn kannst du ein vielfältiges sportliches Programm genießen. Tennisfreunde finden auf der Anlage insgesamt elf Tennisplätze (davon vier Freiplätze). So kannst du witterungs- und jahreszeitunabhängig Tennis genießen. Fünf Badminton Courts und zwei Tischtennistische sorgen für Abwechslung bei Racketsportfans. Außerdem findest du auf der Anlage eine Mehrzweckhalle mit zwei Fußballplätzen (11*30m). Die Halle eignet sich außerdem für Feiern und Feste und kann für solche Zwecke angemietet werden. Auch der Wohlfühlfaktor kommt bei Sport König nicht zu kurz. Nach dem Sport kannst du dich in der Sauna, im Dampfbad oder im Solarium so richtig entspannen. Vor Sport König findest du genügend Parkplätze. Du kannst also ruhig mit dem Auto zum Center anreisen.",
location: {
lat: 48.33144,
long: 16.44997
},
email: "info@sport-koenig.at",
phone: "+43 2246 20 810",
images: [
{
url:
"https://files.eversports.com/963b0af7-1fae-4257-913c-23f27d795a0e/sport_koenig_aia_40819_eversports_1jpg-x-large.jpg",
isBanner: true
},
{
url:
"https://files.eversports.com/e81c744d-89fa-4620-8134-55acd1019565/sport_koenig_aia_40819_eversports_6jpg-x-large.jpg",
isBanner: false
}
]
},
{
id: "2a7ecfb2-28db-4d25-a745-3ef34bab43a3",
name: "Soccer Cup Hall",
description:
"Soccer Cup Hall wurde im Oktober 2016 eröffnet und bietet Fußballvereinen und Hobby-Mannschaften das ideale Umfeld für Fußballtrainings in einer modern ausgestatteten Fußballhall. Außerdem werden Umkleidekabinen, Duschen, kostenfreie Parkplätze und einen gemütlichen Gastronomiebereich angeboten. Es werden regelmäßige Fußballturniere veranstaltet, sowie Geburtstage und viele weitere Events. Sportveranstaltungen werden auch auf großen Flachbildschirmen live vor Ort übertragen.",
location: {
lat: 48.14472,
long: 16.35745
},
email: "office@soccer-cup.at",
phone: "+43 660 152 00 44",
images: [
{
url:
"https://files.eversports.com/46d0b0a9-484e-4a51-b8c9-8e61e6c6712f/soccercup-large-x-large.jpg",
isBanner: true
}
]
},
{
id: "883b35f9-2de7-466d-a12f-a48c08f616ae",
name: "Sportanlage Langenzersdorf",
description:
"Die SportanlageLangenzersdorf in Wien zeichnet sich durch ein breites Sportangebot aus. Im Portfolio der Sport- und Erholungseinrichtung befinden sich neben Klassikern wie Tennis, Fußball und Beachvolleyball auch Sportarten, die es noch zu entdecken gilt. Doch die Anlage ist nicht nur Stätte für Sportler und Erholungsuchende. In ihren Räumlichkeiten können Vereine und Firmen auch Seminare, Fortbildungs- und sonstige Veranstaltungen durchführen.",
location: {
lat: 48.29114,
long: 16.37919
},
email: null,
phone: null,
images: []
},
{
id: "79ddb68d-5919-43b8-ada3-dc285d3deb05",
name: "Tatooine Pod Race Arena",
description:
"Tatooine is a fictional desert planet that appears in the Star Wars space opera franchise. It is beige-coloured and is depicted as a remote, desolate world orbiting a pair of binary stars, and inhabited by human settlers and a variety of other life forms.",
location: {
lat: 30.04442,
long: 31.235712
},
email: "pod-race@tatooine.com",
phone: null,
images: [
{
url:
"https://upload.wikimedia.org/wikipedia/en/0/09/Ep1RacerFlamethrower.jpg",
isBanner: true
}
]
}
]
};
@nikgraf
Copy link
Author

nikgraf commented Nov 20, 2018

screen shot 2018-11-20 at 13 39 47

@nikgraf
Copy link
Author

nikgraf commented Nov 20, 2018

screen shot 2018-11-20 at 15 21 57

@nikgraf
Copy link
Author

nikgraf commented Nov 20, 2018

React Inline Styles
screen shot 2018-11-20 at 16 54 57

css-modules
screen shot 2018-11-20 at 16 55 54

styled-components
screen shot 2018-11-20 at 16 56 03

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment