Thursday, 4th October 2018
Teacher: @thekitze
This repository contains all the commits made during the workshop, below you can find the exercises list.
The repository contains also my code for the exercises, remember:
- I know that it could be improved in a lot of ways but it isn't the goal of this repo
- the exercises are made in an incremental way so I never removed the previous example. In the end, you will find all the exercises together
- you can see the code running on CodeSandbox
- the exercises themselves are useful to you only if you attended the workshop with the amazing @thekitze
ex. 1: Download
- Create a HOC called
withMouse - It should send the mouse position (
xandy) to the underlying component as props
ex. 2: Download
- Write a HOC called
measureTimethat will measure the time while a component is mounted - This HOC should pass a
secondsPassedprop to the original component
ex. 3: Download
- Create the
fetchDataHOC. - Note that this HOC should also accept an url so you need to create a function that returns a HOC that returns a component… fun right?
- Fetch real data using
window.fetch, you can use the Star Wars api:https://swapi.co/api/people/1 - Example usage:
const WrappedApp = fetchData('https://swapi.co/api/people/1')(App);
ex. 1: Download
- Recreate all the HOCs that we made, but using render props.
- The components should be
FetchData,MeasureTimeandMouseTrackerand they should be in acomponentsfolder - You can use the
renderor thechildrenprop, it doesn’t matter. - Use all of them in
App.js
ex. 2: Download
- Create a
KeyLoggercomponent that’s gonna use render props - Focus the
KeyLoggercomponent when it mounts (you can use refs for this) and set thetabIndexto 0 - When the user types something, pass
lastKeystrokeandtypedTextto the children - Don’t save
Backspace,EnterandEscapein thetypedTextprop, but show them inlastKeystroke - Hint: use
onKeyDownto capture keyboard events - Make sure this component wraps everything in
App
ex. 1: Download
- For this exercise you need to create two components:
SelectandOptionUsage: - The
Optioncomponent should be a functional component, and it should only receive props:onSelect,childrenandactive - The
Selectcomponent should be a compound component, and it should have two states: when it’s opened and closed. - You should keep an
openedproperty in the state of theSelectcomponent, but theOptioncomponent shouldn’t care about it Selectshould accept adefaultValueprop which will set the initial value- If
Selectis closed, it should display the currently selectedoption - When clicking on
Selectit should present all the other options, and the selectedoptionshould be highlighted - When clicking on an
option, the state should be updated inSelect - To pass the state from
SelecttoOptionyou should use theReact.cloneElement()api
ex. 1: Download
- Make the
Selectcomponent controlled. - It should accept the following props:
value,opened,onSelect,onOpen - Inside of it, check on every click if the component is controlled or not
- If the component is controlled, you should just call the callbacks (
onSelect,onClick) etc - If the component is not controlled, you should change the state internally
- If the component is controlled, make sure to compare to the outside props
valueandopenedinstead of comparing to the internal state - Keep the state of the Select inside of the
Appcomponent, and just pass it down toSelectto make it controlled