From 769ea7d4c6a44401a07536bfe8fc6e0c7d58798a Mon Sep 17 00:00:00 2001 From: Norbert Nemeth Date: Mon, 25 Apr 2022 09:08:06 +0200 Subject: [PATCH 01/14] docs: Update README with week 1 steps --- README.md | 87 +++++++++++++++++-------------------------------------- 1 file changed, 27 insertions(+), 60 deletions(-) diff --git a/README.md b/README.md index 58beeac..014717f 100644 --- a/README.md +++ b/README.md @@ -1,70 +1,37 @@ -# Getting Started with Create React App +# Bootstrap This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). -## Available Scripts +```sh +npx create-react-app react-workshop-2022 +cd react-workshop-2022 +yarn start +``` +App is now running at [http://localhost:3000](http://localhost:3000). -In the project directory, you can run: +# Goal -### `npm start` +We are going to create an Euro BTC converter application -Runs the app in the development mode.\ -Open [http://localhost:3000](http://localhost:3000) to view it in your browser. +## Week 1 -The page will reload when you make changes.\ -You may also see any lint errors in the console. +1. Add a number input with a label, "Euros". +2. Extract the input into a separate component called that takes a name (eg. "Euros") prop. +3. Teach input to show a red outline for negative amounts. +4. Make a controlled component (ie. pass it its value as a prop). +5. Add a second, read-only component that shows $BTC instead of Euros; use this function to get the exchange rate: +```javascript + function exchangeRate() { + return Math.random() * 10000; + } +``` +6. Use setTimeout to make the $BTC price crash to zero after 5 seconds of inactivity. +7. Use React.createContext() to provide a dark/light theme toggle. -### `npm test` +Help: -Launches the test runner in the interactive watch mode.\ -See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. +(Context API described using React class components) +https://reactjs.org/docs/context.html -### `npm run build` - -Builds the app for production to the `build` folder.\ -It correctly bundles React in production mode and optimizes the build for the best performance. - -The build is minified and the filenames include the hashes.\ -Your app is ready to be deployed! - -See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. - -### `npm run eject` - -**Note: this is a one-way operation. Once you `eject`, you can't go back!** - -If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. - -Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own. - -You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it. - -## Learn More - -You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). - -To learn React, check out the [React documentation](https://reactjs.org/). - -### Code Splitting - -This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) - -### Analyzing the Bundle Size - -This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) - -### Making a Progressive Web App - -This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) - -### Advanced Configuration - -This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) - -### Deployment - -This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) - -### `npm run build` fails to minify - -This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) +(useContext hook and how to use it in functional components) +https://reactjs.org/docs/hooks-reference.html#usecontext From 20d2b429e3cbf3382698c83fb4121f3e6b3f2a62 Mon Sep 17 00:00:00 2001 From: Norbert Nemeth Date: Mon, 25 Apr 2022 09:08:42 +0200 Subject: [PATCH 02/14] chore: clean out app.css and app.js --- src/App.css | 38 -------------------------------------- src/App.js | 15 --------------- 2 files changed, 53 deletions(-) diff --git a/src/App.css b/src/App.css index 74b5e05..e69de29 100644 --- a/src/App.css +++ b/src/App.css @@ -1,38 +0,0 @@ -.App { - text-align: center; -} - -.App-logo { - height: 40vmin; - pointer-events: none; -} - -@media (prefers-reduced-motion: no-preference) { - .App-logo { - animation: App-logo-spin infinite 20s linear; - } -} - -.App-header { - background-color: #282c34; - min-height: 100vh; - display: flex; - flex-direction: column; - align-items: center; - justify-content: center; - font-size: calc(10px + 2vmin); - color: white; -} - -.App-link { - color: #61dafb; -} - -@keyframes App-logo-spin { - from { - transform: rotate(0deg); - } - to { - transform: rotate(360deg); - } -} diff --git a/src/App.js b/src/App.js index 3784575..b08cb38 100644 --- a/src/App.js +++ b/src/App.js @@ -1,23 +1,8 @@ -import logo from './logo.svg'; import './App.css'; function App() { return (
-
- logo -

- Edit src/App.js and save to reload. -

- - Learn React - -
); } From 2df36ec9bada223adae033b07e32cd087b98c203 Mon Sep 17 00:00:00 2001 From: Laszlo Pap Date: Thu, 28 Apr 2022 18:56:17 +0200 Subject: [PATCH 03/14] Add a number input with a label: Euros. --- src/App.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/App.js b/src/App.js index b08cb38..7bfe702 100644 --- a/src/App.js +++ b/src/App.js @@ -3,6 +3,9 @@ import './App.css'; function App() { return (
+
); } From 28695ecf40f846c2b34afd09ce4b21d925d6bdd5 Mon Sep 17 00:00:00 2001 From: Laszlo Pap Date: Thu, 28 Apr 2022 18:59:26 +0200 Subject: [PATCH 04/14] Extract the input into a separate component called 'Amount' that takes a name (eg. Euros) prop. --- src/App.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/App.js b/src/App.js index 7bfe702..b110da1 100644 --- a/src/App.js +++ b/src/App.js @@ -1,10 +1,20 @@ import './App.css'; + +const euroComponent = + function App() { return ( + euroComponent + ); +} + +function Amount({label}) { + return (
-
); From 0e5badce248616cdde31c4e09ae11918830887a9 Mon Sep 17 00:00:00 2001 From: Laszlo Pap Date: Thu, 28 Apr 2022 19:20:06 +0200 Subject: [PATCH 05/14] Teach input to show a red outline for negative amounts., Make a controlled component (ie. pass it its value as a prop). --- src/App.css | 3 +++ src/App.js | 40 +++++++++++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/App.css b/src/App.css index e69de29..04533ec 100644 --- a/src/App.css +++ b/src/App.css @@ -0,0 +1,3 @@ +.error { + border: 5px solid red; +} \ No newline at end of file diff --git a/src/App.js b/src/App.js index b110da1..9c418f0 100644 --- a/src/App.js +++ b/src/App.js @@ -1,23 +1,53 @@ import './App.css'; +import React from 'react'; +const defaultEuroValue=10; +var isRedBorderAdded = false; -const euroComponent = function App() { + + const [mystate, setMyState] = React.useState({euroValue: defaultEuroValue}); + const {euroValue} = mystate; + + const myOnChange = (element) => { + let calcEur = element.value; + + setMyState({ + euroValue: calcEur + }); + } + + const euroComponent = + return ( euroComponent ); } -function Amount({label}) { + +function Amount({label, value, onChange}) { + + if (value >= 0){ + isRedBorderAdded = false; + } else { + isRedBorderAdded = true; + } + return ( -
+
); } -export default App; +export default App; \ No newline at end of file From 81bcb1a5ff3c44a8d0a7fbe569ec5953bef14275 Mon Sep 17 00:00:00 2001 From: Laszlo Pap Date: Thu, 28 Apr 2022 19:42:58 +0200 Subject: [PATCH 06/14] Add a second, read-only component that shows instead of Euros --- src/App.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/App.js b/src/App.js index 9c418f0..2c3769d 100644 --- a/src/App.js +++ b/src/App.js @@ -7,26 +7,30 @@ var isRedBorderAdded = false; function App() { - const [mystate, setMyState] = React.useState({euroValue: defaultEuroValue}); - const {euroValue} = mystate; + const [mystate, setMyState] = React.useState({euroValue: defaultEuroValue, btcValue: 0}); + const {euroValue, btcValue} = mystate; + const exchangeRate = getExchangeRate(); const myOnChange = (element) => { let calcEur = element.value; + let calcBtc = element.value / exchangeRate; setMyState({ - euroValue: calcEur + euroValue: calcEur, + btcValue: calcBtc }); } - const euroComponent = + const euroComponent = + const btcComponent = return ( - euroComponent + React.createElement(React.Fragment,null,euroComponent,btcComponent) ); } -function Amount({label, value, onChange}) { +function Amount({label, value, onChange, isReadOnly}) { if (value >= 0){ isRedBorderAdded = false; @@ -35,9 +39,9 @@ function Amount({label, value, onChange}) { } return ( -
+