├── AddCityInState.js ├── Assignments ├── dotMap-dotFilter-excercise │ ├── dot-map-assignment.txt │ ├── dot-map-filter-assignment3.txt │ └── filter-function-assignment.txt ├── excercise-1-passing-data │ ├── README.md │ ├── instructions.md │ ├── package.json │ ├── public │ │ ├── favicon.ico │ │ ├── index.html │ │ └── manifest.json │ └── src │ │ ├── App.css │ │ ├── App.js │ │ ├── App.test.js │ │ ├── index.css │ │ ├── index.js │ │ ├── logo.svg │ │ └── registerServiceWorker.js ├── excercise-2-passing-data │ ├── README.md │ ├── instructions.md │ ├── package.json │ ├── public │ │ ├── favicon.ico │ │ ├── index.html │ │ └── manifest.json │ └── src │ │ ├── App.css │ │ ├── App.js │ │ ├── App.test.js │ │ ├── index.css │ │ ├── index.js │ │ ├── logo.svg │ │ └── registerServiceWorker.js ├── excercise-3-functional-component │ └── instructions.md ├── excercise-4-managing-state │ ├── README.md │ ├── instructions.md │ ├── package.json │ ├── public │ │ ├── favicon.ico │ │ ├── index.html │ │ └── manifest.json │ └── src │ │ ├── App.css │ │ ├── App.js │ │ ├── App.test.js │ │ ├── index.css │ │ ├── index.js │ │ ├── logo.svg │ │ └── registerServiceWorker.js ├── excercise-5-controlled-component │ ├── README.md │ ├── instructions.md │ ├── package.json │ ├── public │ │ ├── favicon.ico │ │ ├── index.html │ │ └── manifest.json │ └── src │ │ ├── App.css │ │ ├── App.js │ │ ├── App.test.js │ │ ├── index.css │ │ ├── index.js │ │ ├── logo.svg │ │ └── registerServiceWorker.js ├── excercise-6-all-topic-component-1 │ ├── README.md │ ├── instructions.md │ ├── package.json │ ├── public │ │ ├── favicon.ico │ │ ├── index.html │ │ └── manifest.json │ └── src │ │ ├── App.css │ │ ├── App.js │ │ ├── App.test.js │ │ ├── index.css │ │ ├── index.js │ │ ├── logo.svg │ │ └── registerServiceWorker.js ├── excercise-7-all-topic-component-2 │ ├── README.md │ ├── instructions.md │ ├── package.json │ ├── public │ │ ├── favicon.ico │ │ ├── index.html │ │ └── manifest.json │ └── src │ │ ├── App.css │ │ ├── App.js │ │ ├── App.test.js │ │ ├── index.css │ │ ├── index.js │ │ ├── logo.svg │ │ └── registerServiceWorker.js └── excercise-8-all-topic-component-3 │ ├── README.md │ ├── instructions.md │ ├── package-lock.json │ ├── package.json │ ├── public │ ├── favicon.ico │ ├── index.html │ └── manifest.json │ └── src │ ├── App.css │ ├── App.js │ ├── App.test.js │ ├── index.css │ ├── index.js │ ├── logo.svg │ └── registerServiceWorker.js ├── BasicComponentExample.js ├── BasicCompositeComponentExample.js ├── CSS-Styling ├── App.css ├── App.js ├── InlineStyling.js ├── flag.png ├── imgTagExample.js ├── style-component-example.js └── tile-bg.jpg ├── ComponentWithDotFilter.js ├── ComponentWithDotMap.js ├── ConditionalRendering.js ├── DestructureStateAndProps.js ├── DestructuringArray1.js ├── DestructuringArray2.js ├── DestructuringObject1.js ├── DestructuringObject2.js ├── DotFilterExampleCode.js ├── DotMapFunction.js ├── FormExample.js ├── JSXReactElementExample.js ├── PassingProps.js ├── PassingPropsAndFunction.js ├── PassingPropsToComponentExample.js ├── PropTypeExample.js ├── PropTypeInClass.js ├── PropsToFunctionalComp.js ├── README.md ├── RemoveCityFromState.js ├── RouterExamples ├── Index.js ├── SwitchWithRouter.js ├── URLParametersExample.js ├── propsWithRedirectExample.js ├── redirectRouterComponent.js └── simpleRoute.js ├── RoutesExample ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ ├── index.html │ ├── logo192.png │ ├── logo512.png │ ├── manifest.json │ └── robots.txt ├── src │ ├── Components │ │ ├── AboutUs │ │ │ └── AboutUs.js │ │ ├── App.css │ │ ├── App.js │ │ ├── App.test.js │ │ ├── ButtonComponent │ │ │ └── ButtonComponent.js │ │ ├── CityList │ │ │ └── CityList.js │ │ ├── ControlledInputBox │ │ │ └── index.js │ │ ├── ErrorMessage │ │ │ └── ErrorMessage.js │ │ ├── MyComponent │ │ │ └── MyComponent.js │ │ └── MyFunctionalComponent │ │ │ └── MyFunctionalComponent.js │ ├── Routes │ │ └── index.js │ ├── Static │ │ └── Images │ │ │ ├── flag.png │ │ │ └── tile-bg.jpg │ ├── index.css │ ├── index.js │ ├── serviceWorker.js │ └── setupTests.js └── yarn.lock ├── SampleFolderStructure ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ ├── index.html │ ├── logo192.png │ ├── logo512.png │ ├── manifest.json │ └── robots.txt ├── src │ ├── Components │ │ ├── App.css │ │ ├── App.js │ │ ├── App.test.js │ │ ├── ButtonComponent │ │ │ └── ButtonComponent.js │ │ ├── ControlledInputBox │ │ │ └── index.js │ │ ├── ErrorMessage │ │ │ └── ErrorMessage.js │ │ ├── MyComponent │ │ │ └── MyComponent.js │ │ └── MyFunctionalComponent │ │ │ └── MyFunctionalComponent.js │ ├── Static │ │ └── Images │ │ │ ├── flag.png │ │ │ └── tile-bg.jpg │ ├── index.css │ ├── index.js │ ├── serviceWorker.js │ └── setupTests.js └── yarn.lock ├── StateExample.js ├── StateExampleCode.js ├── basicReactElementExample.js ├── componentDidMountExample.js ├── componentDidMountExample2.js ├── componentDidUpdateExample.js ├── componentWillUnmountExample.js ├── getDerivedStateFromProps.js ├── myFirstComponent.js ├── setStateExampleCode.js ├── shouldComponentUpdateExample.js └── stateWithChangeHandlerExample ├── App.js ├── Output.js └── UserInput.js /AddCityInState.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | function ControlledInputBox (props) { 4 | const { handleInputChange, textBoxValue } = props 5 | 6 | return ( 7 | 8 | ) 9 | } 10 | 11 | function ButtonComponent (props) { 12 | const { addCity } = props 13 | return ( 14 | 15 | ) 16 | } 17 | 18 | class MyComponent extends React.Component { 19 | state = { 20 | cityArray: ['Karachi', 'Lahore', 'Peshawar', 'Quetta'], 21 | textBoxValue: '' 22 | } 23 | 24 | handleInputChange = (e) => { 25 | const { value } = e.target 26 | this.setState({ 27 | textBoxValue: value 28 | }) 29 | } 30 | 31 | addCity = () => { 32 | const { textBoxValue } = this.state 33 | 34 | this.setState(prevState => ({ 35 | cityArray: [...prevState.cityArray, textBoxValue], 36 | textBoxValue: '' 37 | })) 38 | } 39 | 40 | render () { 41 | const { cityArray, textBoxValue } = this.state 42 | 43 | return ( 44 | <> 45 | 50 | 51 | this.addCity()}/> 52 | 53 | ) 54 | } 55 | } 56 | 57 | function MyFunctionalComponent (props) { 58 | const { heading } = props 59 | return

{heading}

60 | } 61 | 62 | function App () { 63 | return ( 64 |
65 | 66 | 67 |
68 | ) 69 | } 70 | 71 | export default App 72 | -------------------------------------------------------------------------------- /Assignments/dotMap-dotFilter-excercise/dot-map-assignment.txt: -------------------------------------------------------------------------------- 1 | /* Using .map() 2 | * 3 | * Using the musicData array and .map(): 4 | * - return a string for each item in the array in the following format 5 | * by sold copies 6 | * - store the returned data in a new albumSalesStrings variable 7 | * 8 | * Note: 9 | * - do not delete the musicData variable 10 | * - do not alter any of the musicData content 11 | * - do not format the sales number, leave it as a long string of digits 12 | */ 13 | 14 | const musicData = [ 15 | { artist: 'Adele', name: '25', sales: 1731000 }, 16 | { artist: 'Drake', name: 'Views', sales: 1608000 }, 17 | { artist: 'Beyonce', name: 'Lemonade', sales: 1554000 }, 18 | { artist: 'Chris Stapleton', name: 'Traveller', sales: 1085000 }, 19 | { artist: 'Pentatonix', name: 'A Pentatonix Christmas', sales: 904000 }, 20 | { artist: 'Original Broadway Cast Recording', 21 | name: 'Hamilton: An American Musical', sales: 820000 }, 22 | { artist: 'Twenty One Pilots', name: 'Blurryface', sales: 738000 }, 23 | { artist: 'Prince', name: 'The Very Best of Prince', sales: 668000 }, 24 | { artist: 'Rihanna', name: 'Anti', sales: 603000 }, 25 | { artist: 'Justin Bieber', name: 'Purpose', sales: 554000 } 26 | ]; 27 | 28 | 29 | //* by sold copies 30 | 31 | const albumSalesStrings = musicData.map((mdata) => `${mdata.name} by ${mdata.artist} sold ${mdata.sales} copies`); 32 | 33 | console.log(albumSalesStrings); 34 | 35 | -------------------------------------------------------------------------------- /Assignments/dotMap-dotFilter-excercise/dot-map-filter-assignment3.txt: -------------------------------------------------------------------------------- 1 | /* Combining .filter() and .map() 2 | * 3 | * Using the musicData array, .filter, and .map(): 4 | * - filter the musicData array down to just the albums that have 5 | * sold over 1,000,000 copies 6 | * - on the array returned from .filter(), call .map() 7 | * - use .map() to return a string for each item in the array in the 8 | * following format: " is a great performer" 9 | * - store the array returned form .map() in a new "popular" variable 10 | * 11 | * Note: 12 | * - do not delete the musicData variable 13 | * - do not alter any of the musicData content 14 | */ 15 | 16 | const musicData = [ 17 | { artist: 'Adele', name: '25', sales: 1731000 }, 18 | { artist: 'Drake', name: 'Views', sales: 1608000 }, 19 | { artist: 'Beyonce', name: 'Lemonade', sales: 1554000 }, 20 | { artist: 'Chris Stapleton', name: 'Traveller', sales: 1085000 }, 21 | { artist: 'Pentatonix', name: 'A Pentatonix Christmas', sales: 904000 }, 22 | { artist: 'Original Broadway Cast Recording', 23 | name: 'Hamilton: An American Musical', sales: 820000 }, 24 | { artist: 'Twenty One Pilots', name: 'Blurryface', sales: 738000 }, 25 | { artist: 'Prince', name: 'The Very Best of Prince', sales: 668000 }, 26 | { artist: 'Rihanna', name: 'Anti', sales: 603000 }, 27 | { artist: 'Justin Bieber', name: 'Purpose', sales: 554000 } 28 | ]; 29 | 30 | const popular = 'Replace this message with your code!'; 31 | 32 | console.log(popular); 33 | 34 | -------------------------------------------------------------------------------- /Assignments/dotMap-dotFilter-excercise/filter-function-assignment.txt: -------------------------------------------------------------------------------- 1 | /* Using .filter() 2 | * 3 | * Using the musicData array and .filter(): 4 | * - return only album objects where the album's name is 5 | * 10 characters long, 25 characters long, or anywhere in between 6 | * - store the returned data in a new `results` variable 7 | * 8 | * Note: 9 | * - do not delete the musicData variable 10 | * - do not alter any of the musicData content 11 | */ 12 | 13 | const musicData = [ 14 | { artist: 'Adele', name: '25', sales: 1731000 }, 15 | { artist: 'Drake', name: 'Views', sales: 1608000 }, 16 | { artist: 'Beyonce', name: 'Lemonade', sales: 1554000 }, 17 | { artist: 'Chris Stapleton', name: 'Traveller', sales: 1085000 }, 18 | { artist: 'Pentatonix', name: 'A Pentatonix Christmas', sales: 904000 }, 19 | { artist: 'Original Broadway Cast Recording', 20 | name: 'Hamilton: An American Musical', sales: 820000 }, 21 | { artist: 'Twenty One Pilots', name: 'Blurryface', sales: 738000 }, 22 | { artist: 'Prince', name: 'The Very Best of Prince', sales: 668000 }, 23 | { artist: 'Rihanna', name: 'Anti', sales: 603000 }, 24 | { artist: 'Justin Bieber', name: 'Purpose', sales: 554000 } 25 | ]; 26 | 27 | const results = musicData.filter( mdata => mdata.name.length >= 10 && mdata.name.length <=25); 28 | 29 | console.log(results); 30 | -------------------------------------------------------------------------------- /Assignments/excercise-1-passing-data/instructions.md: -------------------------------------------------------------------------------- 1 | Instructions 2 | 3 | Use React and the `chart`, `peeps`, and `books` data in `App.js` to display a list of people alongside the book they liked. 4 | 5 | Example 6 | 7 | The `peeps` object has the people detail like "Fazal Deen": 8 | 9 | const peeps = { 10 | 1: { 11 | id: 1, 12 | name: 'Fazal Deen', 13 | readerCategory: 'champ', 14 | }, 15 | . 16 | . 17 | . 18 | }; 19 | 20 | And the `chart` array has a combination of which peeps and the book ID the liked: 21 | 22 | 23 | const chart = [ 24 | { 25 | id: 1, 26 | peepsID: '1', 27 | bookLikedID: '1', 28 | }, 29 | . 30 | . 31 | . 32 | ]; 33 | 34 | The `books` object has the book names like "Imran Series": 35 | 36 | const books = { 37 | 1: { 38 | id: 1, 39 | name: 'Imran Series', 40 | }, 41 | 42 | From this information, one of the list items might look like this: 43 | 44 | * Fazal Deen's likes a Imran Series book. 45 | -------------------------------------------------------------------------------- /Assignments/excercise-1-passing-data/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "author-likes-movie", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react": "^16.3.2", 7 | "react-dom": "^16.3.2", 8 | "react-scripts": "1.1.4" 9 | }, 10 | "scripts": { 11 | "start": "react-scripts start", 12 | "build": "react-scripts build", 13 | "test": "react-scripts test --env=jsdom", 14 | "eject": "react-scripts eject" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Assignments/excercise-1-passing-data/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/Assignments/excercise-1-passing-data/public/favicon.ico -------------------------------------------------------------------------------- /Assignments/excercise-1-passing-data/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 12 | 13 | 22 | React App 23 | 24 | 25 | 28 |
29 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Assignments/excercise-1-passing-data/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /Assignments/excercise-1-passing-data/src/App.css: -------------------------------------------------------------------------------- 1 | .App-logo { 2 | animation: App-logo-spin infinite 20s linear; 3 | height: 80px; 4 | } 5 | 6 | .App-header { 7 | background-color: #222; 8 | height: 150px; 9 | padding: 20px; 10 | text-align: center; 11 | color: white; 12 | } 13 | 14 | .App-title { 15 | font-size: 1.5em; 16 | } 17 | 18 | .App-intro { 19 | font-size: large; 20 | } 21 | 22 | @keyframes App-logo-spin { 23 | from { transform: rotate(0deg); } 24 | to { transform: rotate(360deg); } 25 | } 26 | -------------------------------------------------------------------------------- /Assignments/excercise-1-passing-data/src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import './App.css'; 3 | import logo from './logo.svg'; 4 | 5 | /* 6 | Use React and the data below to display a list of people alongside their books they like. 7 | 8 | Fazal Deen's likes a Imran Series book. 9 | 10 | For detailed instructions, refer to instructions.md. 11 | */ 12 | 13 | const chart = [ 14 | { 15 | id: 1, 16 | peepsID: '1', 17 | bookLikedID: '1', 18 | }, 19 | { 20 | id: 2, 21 | peepsID: '2', 22 | bookLikedID: '1', 23 | }, 24 | { 25 | id: 3, 26 | peepsID: '4', 27 | bookLikedID: '5', 28 | }, 29 | { 30 | id: 4, 31 | peepsID: '5', 32 | bookLikedID: '2', 33 | }, 34 | { 35 | id: 5, 36 | peepsID: '3', 37 | bookLikedID: '5', 38 | }, 39 | { 40 | id: 6, 41 | peepsID: '6', 42 | bookLikedID: '4', 43 | }, 44 | ]; 45 | 46 | const peeps = { 47 | 1: { 48 | id: 1, 49 | name: 'Fazal Deen', 50 | readerCategory: 'champ', 51 | }, 52 | 2: { 53 | id: 2, 54 | name: 'Irfan', 55 | readerCategory: 'rising-star', 56 | }, 57 | 3: { 58 | id: 3, 59 | name: 'Muneeb', 60 | readerCategory: 'beginner', 61 | }, 62 | 4: { 63 | id: 4, 64 | name: 'Osama', 65 | readerCategory: 'champ', 66 | }, 67 | 5: { 68 | id: 5, 69 | name: 'Najam', 70 | readerCategory: 'champ', 71 | }, 72 | 6: { 73 | id: 6, 74 | name: 'Aamir', 75 | readerCategory: 'beginner', 76 | }, 77 | }; 78 | 79 | const books = { 80 | 1: { 81 | id: 1, 82 | name: 'Imran Series', 83 | }, 84 | 2: { 85 | id: 2, 86 | name: 'Harry Potter', 87 | }, 88 | 3: { 89 | id: 3, 90 | name: 'I Am Malala', 91 | }, 92 | 4: { 93 | id: 4, 94 | name: 'Bang-e-Dara by Allama Iqbal', 95 | }, 96 | 5: { 97 | id: 5, 98 | name: 'Jokes 101', 99 | }, 100 | }; 101 | 102 | class App extends Component { 103 | render() { 104 | return ( 105 |
106 |
107 | logo 108 |

React Bootcamp - Train the Trainer - Coding Practice

109 |
110 |

Book People liked

111 |
112 | ) 113 | } 114 | } 115 | 116 | export default App; 117 | -------------------------------------------------------------------------------- /Assignments/excercise-1-passing-data/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | ReactDOM.unmountComponentAtNode(div); 9 | }); 10 | -------------------------------------------------------------------------------- /Assignments/excercise-1-passing-data/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /Assignments/excercise-1-passing-data/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import registerServiceWorker from './registerServiceWorker'; 6 | 7 | ReactDOM.render(, document.getElementById('root')); 8 | registerServiceWorker(); 9 | -------------------------------------------------------------------------------- /Assignments/excercise-1-passing-data/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Assignments/excercise-1-passing-data/src/registerServiceWorker.js: -------------------------------------------------------------------------------- 1 | // In production, we register a service worker to serve assets from local cache. 2 | 3 | // This lets the app load faster on subsequent visits in production, and gives 4 | // it offline capabilities. However, it also means that developers (and users) 5 | // will only see deployed updates on the "N+1" visit to a page, since previously 6 | // cached resources are updated in the background. 7 | 8 | // To learn more about the benefits of this model, read https://goo.gl/KwvDNy. 9 | // This link also includes instructions on opting out of this behavior. 10 | 11 | const isLocalhost = Boolean( 12 | window.location.hostname === 'localhost' || 13 | // [::1] is the IPv6 localhost address. 14 | window.location.hostname === '[::1]' || 15 | // 127.0.0.1/8 is considered localhost for IPv4. 16 | window.location.hostname.match( 17 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 18 | ) 19 | ); 20 | 21 | export default function register() { 22 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 23 | // The URL constructor is available in all browsers that support SW. 24 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location); 25 | if (publicUrl.origin !== window.location.origin) { 26 | // Our service worker won't work if PUBLIC_URL is on a different origin 27 | // from what our page is served on. This might happen if a CDN is used to 28 | // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374 29 | return; 30 | } 31 | 32 | window.addEventListener('load', () => { 33 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 34 | 35 | if (isLocalhost) { 36 | // This is running on localhost. Lets check if a service worker still exists or not. 37 | checkValidServiceWorker(swUrl); 38 | 39 | // Add some additional logging to localhost, pointing developers to the 40 | // service worker/PWA documentation. 41 | navigator.serviceWorker.ready.then(() => { 42 | console.log( 43 | 'This web app is being served cache-first by a service ' + 44 | 'worker. To learn more, visit https://goo.gl/SC7cgQ' 45 | ); 46 | }); 47 | } else { 48 | // Is not local host. Just register service worker 49 | registerValidSW(swUrl); 50 | } 51 | }); 52 | } 53 | } 54 | 55 | function registerValidSW(swUrl) { 56 | navigator.serviceWorker 57 | .register(swUrl) 58 | .then(registration => { 59 | registration.onupdatefound = () => { 60 | const installingWorker = registration.installing; 61 | installingWorker.onstatechange = () => { 62 | if (installingWorker.state === 'installed') { 63 | if (navigator.serviceWorker.controller) { 64 | // At this point, the old content will have been purged and 65 | // the fresh content will have been added to the cache. 66 | // It's the perfect time to display a "New content is 67 | // available; please refresh." message in your web app. 68 | console.log('New content is available; please refresh.'); 69 | } else { 70 | // At this point, everything has been precached. 71 | // It's the perfect time to display a 72 | // "Content is cached for offline use." message. 73 | console.log('Content is cached for offline use.'); 74 | } 75 | } 76 | }; 77 | }; 78 | }) 79 | .catch(error => { 80 | console.error('Error during service worker registration:', error); 81 | }); 82 | } 83 | 84 | function checkValidServiceWorker(swUrl) { 85 | // Check if the service worker can be found. If it can't reload the page. 86 | fetch(swUrl) 87 | .then(response => { 88 | // Ensure service worker exists, and that we really are getting a JS file. 89 | if ( 90 | response.status === 404 || 91 | response.headers.get('content-type').indexOf('javascript') === -1 92 | ) { 93 | // No service worker found. Probably a different app. Reload the page. 94 | navigator.serviceWorker.ready.then(registration => { 95 | registration.unregister().then(() => { 96 | window.location.reload(); 97 | }); 98 | }); 99 | } else { 100 | // Service worker found. Proceed as normal. 101 | registerValidSW(swUrl); 102 | } 103 | }) 104 | .catch(() => { 105 | console.log( 106 | 'No internet connection found. App is running in offline mode.' 107 | ); 108 | }); 109 | } 110 | 111 | export function unregister() { 112 | if ('serviceWorker' in navigator) { 113 | navigator.serviceWorker.ready.then(registration => { 114 | registration.unregister(); 115 | }); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /Assignments/excercise-2-passing-data/instructions.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Let's do something a little bit more complicated. Instead of displaying a 4 | list of users and their movies, this time you need to display a list of movies. 5 | 6 | For each book in the list, there are two options: 7 | 8 | 1. If the book has been liked by any of the peeps, then display a list of all of the peeps who said that they liked that book. 9 | 2. If the book has *not* been liked, display some text stating that no one liked this book. 10 | 11 | As you go about tackling this project, try to make the app *modular* by breaking it into resusable React components. 12 | 13 | ## Example 14 | 15 | 16 |

Imran Series

17 |

Liked By:

18 |
    19 |
  • Fazal Deen
  • 20 |
21 | 22 |

Jokes 101

23 |

Liked By:

24 |
    25 |
  • Muneeb
  • 26 |
  • Osama
  • 27 |
28 | 29 |

I Am Malala

30 |

None of the peeps in the chart liked this book

31 | 32 | -------------------------------------------------------------------------------- /Assignments/excercise-2-passing-data/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "author-likes-movie", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react": "^16.3.2", 7 | "react-dom": "^16.3.2", 8 | "react-scripts": "1.1.4" 9 | }, 10 | "scripts": { 11 | "start": "react-scripts start", 12 | "build": "react-scripts build", 13 | "test": "react-scripts test --env=jsdom", 14 | "eject": "react-scripts eject" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Assignments/excercise-2-passing-data/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/Assignments/excercise-2-passing-data/public/favicon.ico -------------------------------------------------------------------------------- /Assignments/excercise-2-passing-data/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 12 | 13 | 22 | React App 23 | 24 | 25 | 28 |
29 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Assignments/excercise-2-passing-data/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /Assignments/excercise-2-passing-data/src/App.css: -------------------------------------------------------------------------------- 1 | .App-logo { 2 | animation: App-logo-spin infinite 20s linear; 3 | height: 80px; 4 | } 5 | 6 | .App-header { 7 | background-color: #222; 8 | height: 150px; 9 | padding: 20px; 10 | text-align: center; 11 | color: white; 12 | } 13 | 14 | .App-title { 15 | font-size: 1.5em; 16 | } 17 | 18 | .App-intro { 19 | font-size: large; 20 | } 21 | 22 | @keyframes App-logo-spin { 23 | from { transform: rotate(0deg); } 24 | to { transform: rotate(360deg); } 25 | } 26 | -------------------------------------------------------------------------------- /Assignments/excercise-2-passing-data/src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import './App.css'; 3 | import logo from './logo.svg'; 4 | 5 | /* 6 | Use React and the data below to display a list of people alongside their books they like. 7 | 8 | Fazal Deen's likes a Imran Series book. 9 | 10 | For detailed instructions, refer to instructions.md. 11 | */ 12 | 13 | const chart = [ 14 | { 15 | id: 1, 16 | peepsID: '1', 17 | bookLikedID: '1', 18 | }, 19 | { 20 | id: 2, 21 | peepsID: '2', 22 | bookLikedID: '1', 23 | }, 24 | { 25 | id: 3, 26 | peepsID: '4', 27 | bookLikedID: '5', 28 | }, 29 | { 30 | id: 4, 31 | peepsID: '5', 32 | bookLikedID: '2', 33 | }, 34 | { 35 | id: 5, 36 | peepsID: '3', 37 | bookLikedID: '5', 38 | }, 39 | { 40 | id: 6, 41 | peepsID: '6', 42 | bookLikedID: '4', 43 | }, 44 | ]; 45 | 46 | const peeps = { 47 | 1: { 48 | id: 1, 49 | name: 'Fazal Deen', 50 | readerCategory: 'champ', 51 | }, 52 | 2: { 53 | id: 2, 54 | name: 'Irfan', 55 | readerCategory: 'rising-star', 56 | }, 57 | 3: { 58 | id: 3, 59 | name: 'Muneeb', 60 | readerCategory: 'beginner', 61 | }, 62 | 4: { 63 | id: 4, 64 | name: 'Osama', 65 | readerCategory: 'champ', 66 | }, 67 | 5: { 68 | id: 5, 69 | name: 'Najam', 70 | readerCategory: 'champ', 71 | }, 72 | 6: { 73 | id: 6, 74 | name: 'Aamir', 75 | readerCategory: 'beginner', 76 | }, 77 | }; 78 | 79 | const books = { 80 | 1: { 81 | id: 1, 82 | name: 'Imran Series', 83 | }, 84 | 2: { 85 | id: 2, 86 | name: 'Harry Potter', 87 | }, 88 | 3: { 89 | id: 3, 90 | name: 'I Am Malala', 91 | }, 92 | 4: { 93 | id: 4, 94 | name: 'Bang-e-Dara by Allama Iqbal', 95 | }, 96 | 5: { 97 | id: 5, 98 | name: 'Jokes 101', 99 | }, 100 | }; 101 | 102 | class App extends Component { 103 | render() { 104 | return ( 105 |
106 |
107 | logo 108 |

React Bootcamp - Train the Trainer - Coding Practice

109 |
110 |

Book People liked

111 |
112 | ); 113 | } 114 | } 115 | 116 | export default App; 117 | -------------------------------------------------------------------------------- /Assignments/excercise-2-passing-data/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | ReactDOM.unmountComponentAtNode(div); 9 | }); 10 | -------------------------------------------------------------------------------- /Assignments/excercise-2-passing-data/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /Assignments/excercise-2-passing-data/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import registerServiceWorker from './registerServiceWorker'; 6 | 7 | ReactDOM.render(, document.getElementById('root')); 8 | registerServiceWorker(); 9 | -------------------------------------------------------------------------------- /Assignments/excercise-2-passing-data/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Assignments/excercise-2-passing-data/src/registerServiceWorker.js: -------------------------------------------------------------------------------- 1 | // In production, we register a service worker to serve assets from local cache. 2 | 3 | // This lets the app load faster on subsequent visits in production, and gives 4 | // it offline capabilities. However, it also means that developers (and users) 5 | // will only see deployed updates on the "N+1" visit to a page, since previously 6 | // cached resources are updated in the background. 7 | 8 | // To learn more about the benefits of this model, read https://goo.gl/KwvDNy. 9 | // This link also includes instructions on opting out of this behavior. 10 | 11 | const isLocalhost = Boolean( 12 | window.location.hostname === 'localhost' || 13 | // [::1] is the IPv6 localhost address. 14 | window.location.hostname === '[::1]' || 15 | // 127.0.0.1/8 is considered localhost for IPv4. 16 | window.location.hostname.match( 17 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 18 | ) 19 | ); 20 | 21 | export default function register() { 22 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 23 | // The URL constructor is available in all browsers that support SW. 24 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location); 25 | if (publicUrl.origin !== window.location.origin) { 26 | // Our service worker won't work if PUBLIC_URL is on a different origin 27 | // from what our page is served on. This might happen if a CDN is used to 28 | // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374 29 | return; 30 | } 31 | 32 | window.addEventListener('load', () => { 33 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 34 | 35 | if (isLocalhost) { 36 | // This is running on localhost. Lets check if a service worker still exists or not. 37 | checkValidServiceWorker(swUrl); 38 | 39 | // Add some additional logging to localhost, pointing developers to the 40 | // service worker/PWA documentation. 41 | navigator.serviceWorker.ready.then(() => { 42 | console.log( 43 | 'This web app is being served cache-first by a service ' + 44 | 'worker. To learn more, visit https://goo.gl/SC7cgQ' 45 | ); 46 | }); 47 | } else { 48 | // Is not local host. Just register service worker 49 | registerValidSW(swUrl); 50 | } 51 | }); 52 | } 53 | } 54 | 55 | function registerValidSW(swUrl) { 56 | navigator.serviceWorker 57 | .register(swUrl) 58 | .then(registration => { 59 | registration.onupdatefound = () => { 60 | const installingWorker = registration.installing; 61 | installingWorker.onstatechange = () => { 62 | if (installingWorker.state === 'installed') { 63 | if (navigator.serviceWorker.controller) { 64 | // At this point, the old content will have been purged and 65 | // the fresh content will have been added to the cache. 66 | // It's the perfect time to display a "New content is 67 | // available; please refresh." message in your web app. 68 | console.log('New content is available; please refresh.'); 69 | } else { 70 | // At this point, everything has been precached. 71 | // It's the perfect time to display a 72 | // "Content is cached for offline use." message. 73 | console.log('Content is cached for offline use.'); 74 | } 75 | } 76 | }; 77 | }; 78 | }) 79 | .catch(error => { 80 | console.error('Error during service worker registration:', error); 81 | }); 82 | } 83 | 84 | function checkValidServiceWorker(swUrl) { 85 | // Check if the service worker can be found. If it can't reload the page. 86 | fetch(swUrl) 87 | .then(response => { 88 | // Ensure service worker exists, and that we really are getting a JS file. 89 | if ( 90 | response.status === 404 || 91 | response.headers.get('content-type').indexOf('javascript') === -1 92 | ) { 93 | // No service worker found. Probably a different app. Reload the page. 94 | navigator.serviceWorker.ready.then(registration => { 95 | registration.unregister().then(() => { 96 | window.location.reload(); 97 | }); 98 | }); 99 | } else { 100 | // Service worker found. Proceed as normal. 101 | registerValidSW(swUrl); 102 | } 103 | }) 104 | .catch(() => { 105 | console.log( 106 | 'No internet connection found. App is running in offline mode.' 107 | ); 108 | }); 109 | } 110 | 111 | export function unregister() { 112 | if ('serviceWorker' in navigator) { 113 | navigator.serviceWorker.ready.then(registration => { 114 | registration.unregister(); 115 | }); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /Assignments/excercise-3-functional-component/instructions.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | Change the Components from the assignment you made from class components to functional stateless components 4 | -------------------------------------------------------------------------------- /Assignments/excercise-4-managing-state/instructions.md: -------------------------------------------------------------------------------- 1 | ### Instructions 2 | 3 | You're given a starter template with dummy data. 4 | 5 | **Task**: You will be creating a "ADD THE NUMBERS GAME" in which you will display 6 | three random number and proposed answer on the screen. This proposed number 7 | could by right answer or could be wrong 8 | 9 | You would be creating two buttons to guess the answer 1) True 2) False 10 | 11 | By looking at a proposed answer from the screen, user will judge for its correctness 12 | based on that judgement answer could be true or false (in both case user answer can 13 | be count correct or incorrect) 14 | 15 | There will be a score counter along with total number of question asked in a following manner 16 | 17 | You have answered [user score count] question answered correctly out of total [total question count] questions. 18 | 19 | Basic working is also provided in App.js file. 20 | 21 | Please make sure that your application must be modular 22 | 23 | This exercise will help you practice almost all the topics we have learned so far specially state management one. 24 | -------------------------------------------------------------------------------- /Assignments/excercise-4-managing-state/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "author-likes-movie", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react": "^16.3.2", 7 | "react-dom": "^16.3.2", 8 | "react-scripts": "1.1.4" 9 | }, 10 | "scripts": { 11 | "start": "react-scripts start", 12 | "build": "react-scripts build", 13 | "test": "react-scripts test --env=jsdom", 14 | "eject": "react-scripts eject" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Assignments/excercise-4-managing-state/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/Assignments/excercise-4-managing-state/public/favicon.ico -------------------------------------------------------------------------------- /Assignments/excercise-4-managing-state/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 12 | 13 | 22 | React App 23 | 24 | 25 | 28 |
29 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Assignments/excercise-4-managing-state/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /Assignments/excercise-4-managing-state/src/App.css: -------------------------------------------------------------------------------- 1 | .App-logo { 2 | animation: App-logo-spin infinite 20s linear; 3 | height: 80px; 4 | } 5 | 6 | .App-header { 7 | background-color: #222; 8 | height: 150px; 9 | padding: 20px; 10 | text-align: center; 11 | color: white; 12 | } 13 | 14 | .App-title { 15 | font-size: 1.5em; 16 | } 17 | 18 | .App-intro { 19 | font-size: large; 20 | } 21 | 22 | @keyframes App-logo-spin { 23 | from { transform: rotate(0deg); } 24 | to { transform: rotate(360deg); } 25 | } 26 | -------------------------------------------------------------------------------- /Assignments/excercise-4-managing-state/src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import logo from './logo.svg'; 3 | import './App.css'; 4 | 5 | const number1 = Math.floor(Math.random() * 100); 6 | const number2 = Math.floor(Math.random() * 100); 7 | const number3 = Math.floor(Math.random() * 100); 8 | const proposedAnswer = Math.floor(Math.random() * 3) + number1 + number2 + number3; 9 | const numQuestions = 0; 10 | const numCorrect = 0; 11 | 12 | class App extends Component { 13 | render() { 14 | return ( 15 |
16 |
17 | logo 18 |

React Bootcamp - Train the Trainer - Coding Practice

19 |
20 |
21 |

ADD THE NUMBERS GAME

22 |
23 |

{`${number1} + ${number2} + ${number3} = ${proposedAnswer}`}

24 |
25 | 26 | 27 |

28 | You have answered {numCorrect} question answered correctly out of total {numQuestions} questions. 29 |

30 |
31 |
32 | ); 33 | } 34 | } 35 | 36 | export default App; 37 | 38 | -------------------------------------------------------------------------------- /Assignments/excercise-4-managing-state/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | ReactDOM.unmountComponentAtNode(div); 9 | }); 10 | -------------------------------------------------------------------------------- /Assignments/excercise-4-managing-state/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /Assignments/excercise-4-managing-state/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import registerServiceWorker from './registerServiceWorker'; 6 | 7 | ReactDOM.render(, document.getElementById('root')); 8 | registerServiceWorker(); 9 | -------------------------------------------------------------------------------- /Assignments/excercise-4-managing-state/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Assignments/excercise-4-managing-state/src/registerServiceWorker.js: -------------------------------------------------------------------------------- 1 | // In production, we register a service worker to serve assets from local cache. 2 | 3 | // This lets the app load faster on subsequent visits in production, and gives 4 | // it offline capabilities. However, it also means that developers (and users) 5 | // will only see deployed updates on the "N+1" visit to a page, since previously 6 | // cached resources are updated in the background. 7 | 8 | // To learn more about the benefits of this model, read https://goo.gl/KwvDNy. 9 | // This link also includes instructions on opting out of this behavior. 10 | 11 | const isLocalhost = Boolean( 12 | window.location.hostname === 'localhost' || 13 | // [::1] is the IPv6 localhost address. 14 | window.location.hostname === '[::1]' || 15 | // 127.0.0.1/8 is considered localhost for IPv4. 16 | window.location.hostname.match( 17 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 18 | ) 19 | ); 20 | 21 | export default function register() { 22 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 23 | // The URL constructor is available in all browsers that support SW. 24 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location); 25 | if (publicUrl.origin !== window.location.origin) { 26 | // Our service worker won't work if PUBLIC_URL is on a different origin 27 | // from what our page is served on. This might happen if a CDN is used to 28 | // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374 29 | return; 30 | } 31 | 32 | window.addEventListener('load', () => { 33 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 34 | 35 | if (isLocalhost) { 36 | // This is running on localhost. Lets check if a service worker still exists or not. 37 | checkValidServiceWorker(swUrl); 38 | 39 | // Add some additional logging to localhost, pointing developers to the 40 | // service worker/PWA documentation. 41 | navigator.serviceWorker.ready.then(() => { 42 | console.log( 43 | 'This web app is being served cache-first by a service ' + 44 | 'worker. To learn more, visit https://goo.gl/SC7cgQ' 45 | ); 46 | }); 47 | } else { 48 | // Is not local host. Just register service worker 49 | registerValidSW(swUrl); 50 | } 51 | }); 52 | } 53 | } 54 | 55 | function registerValidSW(swUrl) { 56 | navigator.serviceWorker 57 | .register(swUrl) 58 | .then(registration => { 59 | registration.onupdatefound = () => { 60 | const installingWorker = registration.installing; 61 | installingWorker.onstatechange = () => { 62 | if (installingWorker.state === 'installed') { 63 | if (navigator.serviceWorker.controller) { 64 | // At this point, the old content will have been purged and 65 | // the fresh content will have been added to the cache. 66 | // It's the perfect time to display a "New content is 67 | // available; please refresh." message in your web app. 68 | console.log('New content is available; please refresh.'); 69 | } else { 70 | // At this point, everything has been precached. 71 | // It's the perfect time to display a 72 | // "Content is cached for offline use." message. 73 | console.log('Content is cached for offline use.'); 74 | } 75 | } 76 | }; 77 | }; 78 | }) 79 | .catch(error => { 80 | console.error('Error during service worker registration:', error); 81 | }); 82 | } 83 | 84 | function checkValidServiceWorker(swUrl) { 85 | // Check if the service worker can be found. If it can't reload the page. 86 | fetch(swUrl) 87 | .then(response => { 88 | // Ensure service worker exists, and that we really are getting a JS file. 89 | if ( 90 | response.status === 404 || 91 | response.headers.get('content-type').indexOf('javascript') === -1 92 | ) { 93 | // No service worker found. Probably a different app. Reload the page. 94 | navigator.serviceWorker.ready.then(registration => { 95 | registration.unregister().then(() => { 96 | window.location.reload(); 97 | }); 98 | }); 99 | } else { 100 | // Service worker found. Proceed as normal. 101 | registerValidSW(swUrl); 102 | } 103 | }) 104 | .catch(() => { 105 | console.log( 106 | 'No internet connection found. App is running in offline mode.' 107 | ); 108 | }); 109 | } 110 | 111 | export function unregister() { 112 | if ('serviceWorker' in navigator) { 113 | navigator.serviceWorker.ready.then(registration => { 114 | registration.unregister(); 115 | }); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /Assignments/excercise-5-controlled-component/instructions.md: -------------------------------------------------------------------------------- 1 | ### Instructions 2 | 3 | **Task**: This app should reflect all the text you typed in input field. If there is nothing in input box, nothing should be printed to the page. 4 | 5 | Remember forms in react component could also controls what happens in that form when user inputs something. 6 | 7 | This exercise all about Controlled Components. 8 | 9 | -------------------------------------------------------------------------------- /Assignments/excercise-5-controlled-component/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "author-likes-movie", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react": "^16.3.2", 7 | "react-dom": "^16.3.2", 8 | "react-scripts": "1.1.4" 9 | }, 10 | "scripts": { 11 | "start": "react-scripts start", 12 | "build": "react-scripts build", 13 | "test": "react-scripts test --env=jsdom", 14 | "eject": "react-scripts eject" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Assignments/excercise-5-controlled-component/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/Assignments/excercise-5-controlled-component/public/favicon.ico -------------------------------------------------------------------------------- /Assignments/excercise-5-controlled-component/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 12 | 13 | 22 | React App 23 | 24 | 25 | 28 |
29 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Assignments/excercise-5-controlled-component/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /Assignments/excercise-5-controlled-component/src/App.css: -------------------------------------------------------------------------------- 1 | .App-logo { 2 | animation: App-logo-spin infinite 20s linear; 3 | height: 80px; 4 | } 5 | 6 | .App-header { 7 | background-color: #222; 8 | height: 150px; 9 | padding: 20px; 10 | text-align: center; 11 | color: white; 12 | } 13 | 14 | .App-title { 15 | font-size: 1.5em; 16 | } 17 | 18 | .App-intro { 19 | font-size: large; 20 | } 21 | 22 | @keyframes App-logo-spin { 23 | from { transform: rotate(0deg); } 24 | to { transform: rotate(360deg); } 25 | } 26 | -------------------------------------------------------------------------------- /Assignments/excercise-5-controlled-component/src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import logo from './logo.svg'; 3 | import './App.css'; 4 | 5 | class App extends Component { 6 | render() { 7 | return ( 8 |
9 |
10 | logo 11 |

React Bootcamp - Train the Trainer - Coding Excercise

12 |
13 |
14 | 15 |

You are saying:

16 |

What ever you type into the input field it should reflect here.

17 |
18 |
19 | ); 20 | } 21 | } 22 | 23 | export default App; 24 | 25 | -------------------------------------------------------------------------------- /Assignments/excercise-5-controlled-component/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | ReactDOM.unmountComponentAtNode(div); 9 | }); 10 | -------------------------------------------------------------------------------- /Assignments/excercise-5-controlled-component/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /Assignments/excercise-5-controlled-component/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import registerServiceWorker from './registerServiceWorker'; 6 | 7 | ReactDOM.render(, document.getElementById('root')); 8 | registerServiceWorker(); 9 | -------------------------------------------------------------------------------- /Assignments/excercise-5-controlled-component/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Assignments/excercise-5-controlled-component/src/registerServiceWorker.js: -------------------------------------------------------------------------------- 1 | // In production, we register a service worker to serve assets from local cache. 2 | 3 | // This lets the app load faster on subsequent visits in production, and gives 4 | // it offline capabilities. However, it also means that developers (and users) 5 | // will only see deployed updates on the "N+1" visit to a page, since previously 6 | // cached resources are updated in the background. 7 | 8 | // To learn more about the benefits of this model, read https://goo.gl/KwvDNy. 9 | // This link also includes instructions on opting out of this behavior. 10 | 11 | const isLocalhost = Boolean( 12 | window.location.hostname === 'localhost' || 13 | // [::1] is the IPv6 localhost address. 14 | window.location.hostname === '[::1]' || 15 | // 127.0.0.1/8 is considered localhost for IPv4. 16 | window.location.hostname.match( 17 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 18 | ) 19 | ); 20 | 21 | export default function register() { 22 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 23 | // The URL constructor is available in all browsers that support SW. 24 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location); 25 | if (publicUrl.origin !== window.location.origin) { 26 | // Our service worker won't work if PUBLIC_URL is on a different origin 27 | // from what our page is served on. This might happen if a CDN is used to 28 | // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374 29 | return; 30 | } 31 | 32 | window.addEventListener('load', () => { 33 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 34 | 35 | if (isLocalhost) { 36 | // This is running on localhost. Lets check if a service worker still exists or not. 37 | checkValidServiceWorker(swUrl); 38 | 39 | // Add some additional logging to localhost, pointing developers to the 40 | // service worker/PWA documentation. 41 | navigator.serviceWorker.ready.then(() => { 42 | console.log( 43 | 'This web app is being served cache-first by a service ' + 44 | 'worker. To learn more, visit https://goo.gl/SC7cgQ' 45 | ); 46 | }); 47 | } else { 48 | // Is not local host. Just register service worker 49 | registerValidSW(swUrl); 50 | } 51 | }); 52 | } 53 | } 54 | 55 | function registerValidSW(swUrl) { 56 | navigator.serviceWorker 57 | .register(swUrl) 58 | .then(registration => { 59 | registration.onupdatefound = () => { 60 | const installingWorker = registration.installing; 61 | installingWorker.onstatechange = () => { 62 | if (installingWorker.state === 'installed') { 63 | if (navigator.serviceWorker.controller) { 64 | // At this point, the old content will have been purged and 65 | // the fresh content will have been added to the cache. 66 | // It's the perfect time to display a "New content is 67 | // available; please refresh." message in your web app. 68 | console.log('New content is available; please refresh.'); 69 | } else { 70 | // At this point, everything has been precached. 71 | // It's the perfect time to display a 72 | // "Content is cached for offline use." message. 73 | console.log('Content is cached for offline use.'); 74 | } 75 | } 76 | }; 77 | }; 78 | }) 79 | .catch(error => { 80 | console.error('Error during service worker registration:', error); 81 | }); 82 | } 83 | 84 | function checkValidServiceWorker(swUrl) { 85 | // Check if the service worker can be found. If it can't reload the page. 86 | fetch(swUrl) 87 | .then(response => { 88 | // Ensure service worker exists, and that we really are getting a JS file. 89 | if ( 90 | response.status === 404 || 91 | response.headers.get('content-type').indexOf('javascript') === -1 92 | ) { 93 | // No service worker found. Probably a different app. Reload the page. 94 | navigator.serviceWorker.ready.then(registration => { 95 | registration.unregister().then(() => { 96 | window.location.reload(); 97 | }); 98 | }); 99 | } else { 100 | // Service worker found. Proceed as normal. 101 | registerValidSW(swUrl); 102 | } 103 | }) 104 | .catch(() => { 105 | console.log( 106 | 'No internet connection found. App is running in offline mode.' 107 | ); 108 | }); 109 | } 110 | 111 | export function unregister() { 112 | if ('serviceWorker' in navigator) { 113 | navigator.serviceWorker.ready.then(registration => { 114 | registration.unregister(); 115 | }); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /Assignments/excercise-6-all-topic-component-1/instructions.md: -------------------------------------------------------------------------------- 1 | ### Instructions 2 | 3 | This is the running app. You need to modify it to make it modular and do stuff like passing data into 4 | components, create Stateless Functional Components where appropriate, add state to components where 5 | needed, updating state functionality if state added to component, and create Controlled Components if needed. 6 | 7 | -------------------------------------------------------------------------------- /Assignments/excercise-6-all-topic-component-1/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "author-likes-movie", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react": "^16.3.2", 7 | "react-dom": "^16.3.2", 8 | "react-scripts": "1.1.4" 9 | }, 10 | "scripts": { 11 | "start": "react-scripts start", 12 | "build": "react-scripts build", 13 | "test": "react-scripts test --env=jsdom", 14 | "eject": "react-scripts eject" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Assignments/excercise-6-all-topic-component-1/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/Assignments/excercise-6-all-topic-component-1/public/favicon.ico -------------------------------------------------------------------------------- /Assignments/excercise-6-all-topic-component-1/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 12 | 13 | 22 | React App 23 | 24 | 25 | 28 |
29 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Assignments/excercise-6-all-topic-component-1/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /Assignments/excercise-6-all-topic-component-1/src/App.css: -------------------------------------------------------------------------------- 1 | .App-logo { 2 | animation: App-logo-spin infinite 20s linear; 3 | height: 80px; 4 | } 5 | 6 | .App-header { 7 | background-color: #222; 8 | height: 150px; 9 | padding: 20px; 10 | text-align: center; 11 | color: white; 12 | } 13 | 14 | .App-title { 15 | font-size: 1.5em; 16 | } 17 | 18 | .App-intro { 19 | font-size: large; 20 | } 21 | 22 | @keyframes App-logo-spin { 23 | from { transform: rotate(0deg); } 24 | to { transform: rotate(360deg); } 25 | } 26 | -------------------------------------------------------------------------------- /Assignments/excercise-6-all-topic-component-1/src/App.js: -------------------------------------------------------------------------------- 1 | 2 | import React from 'react'; 3 | import logo from './logo.svg'; 4 | import './App.css'; 5 | 6 | class App extends React.Component { 7 | state = { 8 | value: '', 9 | itemList: [], 10 | }; 11 | 12 | handleChange = event => { 13 | this.setState({ value: event.target.value }); 14 | }; 15 | 16 | addNewItem = event => { 17 | event.preventDefault(); 18 | this.setState(oldState => ({ 19 | itemList: [...oldState.itemList, this.state.value], 20 | })); 21 | }; 22 | 23 | removeLastItem = event => { 24 | this.setState(prevState => ({ itemList: this.state.itemList.slice(0, -1) })); 25 | }; 26 | 27 | inputIsEmpty = () => { 28 | return this.state.value === ''; 29 | }; 30 | 31 | noItemsFound = () => { 32 | return this.state.itemList.length === 0; 33 | }; 34 | 35 | render() { 36 | return ( 37 |
38 |
39 | logo 40 |

React Bootcamp - Train the Trainer - Coding Excercise

41 |
42 |

ToDo List

43 |
44 | 50 | 51 |
52 | 53 | 56 | 57 |

Item List

58 |
    59 | {this.state.itemList.map((item, index) =>
  1. {item}
  2. )} 60 |
61 |
62 | ); 63 | } 64 | } 65 | 66 | export default App; 67 | 68 | 69 | -------------------------------------------------------------------------------- /Assignments/excercise-6-all-topic-component-1/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | ReactDOM.unmountComponentAtNode(div); 9 | }); 10 | -------------------------------------------------------------------------------- /Assignments/excercise-6-all-topic-component-1/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /Assignments/excercise-6-all-topic-component-1/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import registerServiceWorker from './registerServiceWorker'; 6 | 7 | ReactDOM.render(, document.getElementById('root')); 8 | registerServiceWorker(); 9 | -------------------------------------------------------------------------------- /Assignments/excercise-6-all-topic-component-1/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Assignments/excercise-6-all-topic-component-1/src/registerServiceWorker.js: -------------------------------------------------------------------------------- 1 | // In production, we register a service worker to serve assets from local cache. 2 | 3 | // This lets the app load faster on subsequent visits in production, and gives 4 | // it offline capabilities. However, it also means that developers (and users) 5 | // will only see deployed updates on the "N+1" visit to a page, since previously 6 | // cached resources are updated in the background. 7 | 8 | // To learn more about the benefits of this model, read https://goo.gl/KwvDNy. 9 | // This link also includes instructions on opting out of this behavior. 10 | 11 | const isLocalhost = Boolean( 12 | window.location.hostname === 'localhost' || 13 | // [::1] is the IPv6 localhost address. 14 | window.location.hostname === '[::1]' || 15 | // 127.0.0.1/8 is considered localhost for IPv4. 16 | window.location.hostname.match( 17 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 18 | ) 19 | ); 20 | 21 | export default function register() { 22 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 23 | // The URL constructor is available in all browsers that support SW. 24 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location); 25 | if (publicUrl.origin !== window.location.origin) { 26 | // Our service worker won't work if PUBLIC_URL is on a different origin 27 | // from what our page is served on. This might happen if a CDN is used to 28 | // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374 29 | return; 30 | } 31 | 32 | window.addEventListener('load', () => { 33 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 34 | 35 | if (isLocalhost) { 36 | // This is running on localhost. Lets check if a service worker still exists or not. 37 | checkValidServiceWorker(swUrl); 38 | 39 | // Add some additional logging to localhost, pointing developers to the 40 | // service worker/PWA documentation. 41 | navigator.serviceWorker.ready.then(() => { 42 | console.log( 43 | 'This web app is being served cache-first by a service ' + 44 | 'worker. To learn more, visit https://goo.gl/SC7cgQ' 45 | ); 46 | }); 47 | } else { 48 | // Is not local host. Just register service worker 49 | registerValidSW(swUrl); 50 | } 51 | }); 52 | } 53 | } 54 | 55 | function registerValidSW(swUrl) { 56 | navigator.serviceWorker 57 | .register(swUrl) 58 | .then(registration => { 59 | registration.onupdatefound = () => { 60 | const installingWorker = registration.installing; 61 | installingWorker.onstatechange = () => { 62 | if (installingWorker.state === 'installed') { 63 | if (navigator.serviceWorker.controller) { 64 | // At this point, the old content will have been purged and 65 | // the fresh content will have been added to the cache. 66 | // It's the perfect time to display a "New content is 67 | // available; please refresh." message in your web app. 68 | console.log('New content is available; please refresh.'); 69 | } else { 70 | // At this point, everything has been precached. 71 | // It's the perfect time to display a 72 | // "Content is cached for offline use." message. 73 | console.log('Content is cached for offline use.'); 74 | } 75 | } 76 | }; 77 | }; 78 | }) 79 | .catch(error => { 80 | console.error('Error during service worker registration:', error); 81 | }); 82 | } 83 | 84 | function checkValidServiceWorker(swUrl) { 85 | // Check if the service worker can be found. If it can't reload the page. 86 | fetch(swUrl) 87 | .then(response => { 88 | // Ensure service worker exists, and that we really are getting a JS file. 89 | if ( 90 | response.status === 404 || 91 | response.headers.get('content-type').indexOf('javascript') === -1 92 | ) { 93 | // No service worker found. Probably a different app. Reload the page. 94 | navigator.serviceWorker.ready.then(registration => { 95 | registration.unregister().then(() => { 96 | window.location.reload(); 97 | }); 98 | }); 99 | } else { 100 | // Service worker found. Proceed as normal. 101 | registerValidSW(swUrl); 102 | } 103 | }) 104 | .catch(() => { 105 | console.log( 106 | 'No internet connection found. App is running in offline mode.' 107 | ); 108 | }); 109 | } 110 | 111 | export function unregister() { 112 | if ('serviceWorker' in navigator) { 113 | navigator.serviceWorker.ready.then(registration => { 114 | registration.unregister(); 115 | }); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /Assignments/excercise-7-all-topic-component-2/instructions.md: -------------------------------------------------------------------------------- 1 | ### Instructions 2 | create an app in modular form and do stuff like passing data into 3 | components, create Stateless Functional Components where appropriate, add state to components where 4 | needed, updating state functionality if state added to component, and create Controlled Components if needed. 5 | 6 | **Task**: 7 | App should taker user name as input and show the list below as follows 8 | 9 | {user name} has played 0 games. 10 | 11 | 12 | Make a button and place it somewhere. Text the button "Hide no of games played by user". When user clicks on 13 | that button text of button changes to "Show no of games user played" and the format of list displayed earlier changes to as follows 14 | 15 | 16 | {user name} has played * games. 17 | 18 | -------------------------------------------------------------------------------- /Assignments/excercise-7-all-topic-component-2/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "author-likes-movie", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react": "^16.3.2", 7 | "react-dom": "^16.3.2", 8 | "react-scripts": "1.1.4" 9 | }, 10 | "scripts": { 11 | "start": "react-scripts start", 12 | "build": "react-scripts build", 13 | "test": "react-scripts test --env=jsdom", 14 | "eject": "react-scripts eject" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Assignments/excercise-7-all-topic-component-2/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/Assignments/excercise-7-all-topic-component-2/public/favicon.ico -------------------------------------------------------------------------------- /Assignments/excercise-7-all-topic-component-2/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 12 | 13 | 22 | React App 23 | 24 | 25 | 28 |
29 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Assignments/excercise-7-all-topic-component-2/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /Assignments/excercise-7-all-topic-component-2/src/App.css: -------------------------------------------------------------------------------- 1 | .App-logo { 2 | animation: App-logo-spin infinite 20s linear; 3 | height: 80px; 4 | } 5 | 6 | .App-header { 7 | background-color: #222; 8 | height: 150px; 9 | padding: 20px; 10 | text-align: center; 11 | color: white; 12 | } 13 | 14 | .App-title { 15 | font-size: 1.5em; 16 | } 17 | 18 | .App-intro { 19 | font-size: large; 20 | } 21 | 22 | @keyframes App-logo-spin { 23 | from { transform: rotate(0deg); } 24 | to { transform: rotate(360deg); } 25 | } 26 | -------------------------------------------------------------------------------- /Assignments/excercise-7-all-topic-component-2/src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import logo from './logo.svg'; 3 | import './App.css'; 4 | 5 | class App extends Component { 6 | render() { 7 | return ( 8 |
9 |
10 | logo 11 |

React Bootcamp - Train the Trainer - Coding Excercise

12 |
13 | 14 | // CODE HERE 15 | 16 |
17 | ); 18 | } 19 | } 20 | 21 | export default App; 22 | 23 | -------------------------------------------------------------------------------- /Assignments/excercise-7-all-topic-component-2/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | ReactDOM.unmountComponentAtNode(div); 9 | }); 10 | -------------------------------------------------------------------------------- /Assignments/excercise-7-all-topic-component-2/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /Assignments/excercise-7-all-topic-component-2/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import registerServiceWorker from './registerServiceWorker'; 6 | 7 | ReactDOM.render(, document.getElementById('root')); 8 | registerServiceWorker(); 9 | -------------------------------------------------------------------------------- /Assignments/excercise-7-all-topic-component-2/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Assignments/excercise-8-all-topic-component-3/instructions.md: -------------------------------------------------------------------------------- 1 | 2 | ### Instructions 3 | 4 | You're given a starter template with dummy data. 5 | 6 | **Task**: Add interactivity to the app by refactoring the static code in this 7 | template. Make a app that shows 2 Ghupshap chat windows for the 8 | users there in the users array - For example there are currently two users, Aamir Pinger and you in the array. The messages they send to each other should 9 | appear in both chat windows. On Aamir Pinger's screen, his messages should appear in green and 10 | your's messages should appear in blue. On your's screen, your messages should appear in 11 | green and Aamir Pinger's messages should appear in blue. 12 | 13 | Remember to follow these steps from the [Thinking in React Guide](https://reactjs.org/docs/thinking-in-react.html) when you're building your 14 | React applications: 15 | 16 | #### Step 1. Break down the app into a hierarchy of components. Draw a box around each React component. 17 | 18 | #### Step 2. Determine the data in our app. 19 | 20 | #### Step 3. Figure out the data that should be a part of our state: 21 | 22 | 1. Is it passed in from a parent via props? If so, it probably isn’t state. 23 | 24 | 2. Does it remain unchanged over time? If so, it probably isn’t state. 25 | 26 | 3. Can you compute it based on any other state or props in your component? 27 | If so, it isn’t state. 28 | 29 | #### Step 4. Identify where each piece of state lives. 30 | 31 | 1. Identify every component that renders something based on that state. 32 | 33 | 2. If multiple components need the same piece of state, put that piece of state into those components' parent-most component. 34 | 35 | If you can’t find a component where it makes sense to own the state, create 36 | a new component simply for holding the state and add it somewhere in the 37 | hierarchy above the common owner component. 38 | 39 | #### Step 5. Add Inverse Data Flow. 40 | 41 | State should be updated inside of the component where that state lives. 42 | If we pass state down from component A to component B and then need to update 43 | the state based on something that happened in component B, we can do so via 44 | callbacks: Component A will not only pass state to Component B, but it will 45 | also pass a callback function that will fire whenever the state should be updated. 46 | -------------------------------------------------------------------------------- /Assignments/excercise-8-all-topic-component-3/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "8-putting-it-all-together-chat", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "bootstrap": "^4.1.0", 7 | "prop-types": "^15.6.1", 8 | "react": "^16.3.2", 9 | "react-dom": "^16.3.2", 10 | "react-scripts": "^2.1.1" 11 | }, 12 | "scripts": { 13 | "start": "react-scripts start", 14 | "build": "react-scripts build", 15 | "test": "react-scripts test --env=jsdom", 16 | "eject": "react-scripts eject" 17 | }, 18 | "browserslist": [ 19 | ">0.2%", 20 | "not dead", 21 | "not ie <= 11", 22 | "not op_mini all" 23 | ] 24 | } 25 | -------------------------------------------------------------------------------- /Assignments/excercise-8-all-topic-component-3/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/Assignments/excercise-8-all-topic-component-3/public/favicon.ico -------------------------------------------------------------------------------- /Assignments/excercise-8-all-topic-component-3/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 12 | 13 | 22 | React App 23 | 24 | 25 | 28 |
29 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Assignments/excercise-8-all-topic-component-3/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /Assignments/excercise-8-all-topic-component-3/src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .shell { 6 | display: flex; 7 | margin-top: 5%; 8 | } 9 | 10 | .chat-window { 11 | text-align: center; 12 | width: 500px; 13 | height: 100%; 14 | margin: auto; 15 | border: 15px solid #2eb9ce; 16 | padding: 20px; 17 | border-radius: 10px; 18 | color: #019ad2; 19 | font-size: 1.2em; 20 | } 21 | 22 | .centered { 23 | position: fixed; 24 | left: 50%; 25 | } 26 | 27 | .App-logo { 28 | animation: App-logo-spin infinite 20s linear; 29 | height: 80px; 30 | } 31 | 32 | .App-header { 33 | background-color: #222; 34 | height: 150px; 35 | padding: 20px; 36 | color: white; 37 | } 38 | 39 | .App-title { 40 | font-size: 1.5em; 41 | } 42 | 43 | .App-intro { 44 | font-size: large; 45 | } 46 | 47 | @keyframes App-logo-spin { 48 | from { 49 | transform: rotate(0deg); 50 | } 51 | to { 52 | transform: rotate(360deg); 53 | } 54 | } 55 | 56 | .message-list { 57 | list-style-type: none; 58 | height: 80%; 59 | border: 1px solid; 60 | text-align: left; 61 | min-height: 400px; 62 | border-radius: 10px; 63 | padding-top: 1em; 64 | } 65 | 66 | .name { 67 | font-size: 3em; 68 | } 69 | 70 | h2 { 71 | font-size: 2.2em; 72 | text-align: center !important; 73 | } 74 | 75 | .input-group, 76 | .input-group-append { 77 | border-radius: 10px; 78 | color: #2eb9ce !important; 79 | } 80 | 81 | .submit-button { 82 | background-color: #2eb9ce; 83 | color: black; 84 | } 85 | 86 | .submit-button:hover, 87 | .submit-button:active, 88 | .submit-button:focus { 89 | border-color: #2eb9ce; 90 | color: black; 91 | outline: none !important; 92 | box-shadow: none; 93 | background-color: #3498db; 94 | } 95 | 96 | .form-control, 97 | .form-control:focus { 98 | border-color: #2eb9ce; 99 | outline: 0 !important; 100 | -webkit-box-shadow: none; 101 | box-shadow: none; 102 | color: black; 103 | } 104 | 105 | .sender { 106 | color: #29a979; 107 | } 108 | 109 | .recipient { 110 | color: #019ad2; 111 | } 112 | 113 | -------------------------------------------------------------------------------- /Assignments/excercise-8-all-topic-component-3/src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import logo from './logo.svg'; 3 | import './App.css'; 4 | 5 | const users = [{ username: 'your_name' }, { username: 'Aamir_Pinger' }]; 6 | 7 | const messages = [ 8 | { username: 'your_name', text: 'Hi, Aamir!' }, 9 | { username: 'your_name', text: 'How are you?' }, 10 | { username: 'Aamir_Pinger', text: 'Hello, {your_name}! Good n you?' }, 11 | ]; 12 | 13 | class App extends Component { 14 | /* 15 | If the user did not type anything, he/she should not be 16 | allowed to submit. 17 | */ 18 | isDisabled = () => { 19 | return false; 20 | }; 21 | 22 | render() { 23 | return ( 24 |
25 |
26 | logo 27 |

React Bootcamp - Train the Trainer - Coding Excercise

28 |
29 |
30 |
31 |

GhupShap Chat App

32 |
{users[0].username}
33 | 34 |
    35 | {messages.map((message, index) => ( 36 |
  • 42 |

    {`${message.username}: ${message.text}`}

    43 |
  • 44 | ))} 45 |
46 | 47 |
48 |
49 | 50 |
51 | 54 |
55 |
56 |
57 |
58 | 59 |
60 |

GhupShap Chat App

61 |
{users[1].username}
62 |
    63 | {messages.map((message, index) => ( 64 |
  • 70 |

    {`${message.username}: ${message.text}`}

    71 |
  • 72 | ))} 73 |
74 | 75 |
76 |
77 | 78 |
79 | 82 |
83 |
84 |
85 |
86 |
87 |
88 | ); 89 | } 90 | } 91 | 92 | export default App; 93 | 94 | 95 | -------------------------------------------------------------------------------- /Assignments/excercise-8-all-topic-component-3/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | ReactDOM.unmountComponentAtNode(div); 9 | }); 10 | -------------------------------------------------------------------------------- /Assignments/excercise-8-all-topic-component-3/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /Assignments/excercise-8-all-topic-component-3/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import registerServiceWorker from './registerServiceWorker'; 6 | 7 | ReactDOM.render(, document.getElementById('root')); 8 | registerServiceWorker(); 9 | -------------------------------------------------------------------------------- /Assignments/excercise-8-all-topic-component-3/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /BasicComponentExample.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | 4 | class MyFirstReactComponent extends React.Component { 5 | render() { 6 | const cityArray = ["Karachi", "Lahore", "Peshawer", "Quetta"] 7 | 8 | return ( 9 |
    10 | { 11 | cityArray.map((city) =>
  • {city}
  • ) 12 | } 13 |
14 | )} 15 | } 16 | 17 | ReactDOM.render( 18 | , 19 | document.getElementById('root') 20 | ); 21 | -------------------------------------------------------------------------------- /BasicCompositeComponentExample.js: -------------------------------------------------------------------------------- 1 | /* 2 | // Simple component 3 | 4 | import React, { Component } from 'react'; 5 | 6 | class App extends Component { 7 | cities = ["Karachi", "Lahore", "Peshawer", "Quetta"], 8 | 9 | render() { 10 | return ( 11 |
    12 | cities.map((city) =>
  • {city}
  • 13 |
14 | ); 15 | } 16 | } 17 | 18 | export default App 19 | 20 | */ 21 | 22 | // *************************************************** 23 | // 24 | // Composite component Example 25 | // 26 | // *************************************************** 27 | 28 | import React, { Component } from 'react'; 29 | 30 | class City extends Component { 31 | cities = ["Karachi", "Lahore", "Peshawer", "Quetta"], 32 | 33 | render() { 34 | return ( 35 |
    36 | cities.map((city) =>
  • {city}
  • 37 |
38 | ); 39 | } 40 | } 41 | 42 | class Country extends Component { 43 | countries = ["Pakistan", "USA", "UK", "Turkey"], 44 | 45 | render() { 46 | return ( 47 |
    48 | countries.map((country) =>
  • {country}
  • 49 |
50 | ); 51 | } 52 | } 53 | 54 | class App extends Component { 55 | render() { 56 | return ( 57 | 58 | 59 | 60 | ); 61 | } 62 | } 63 | export default App 64 | -------------------------------------------------------------------------------- /CSS-Styling/App.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-size: 16px; 3 | font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif; 4 | } 5 | 6 | h1 { 7 | color: navy; 8 | text-decoration: underline; 9 | } 10 | 11 | .li-styling { 12 | margin: 10px; 13 | } -------------------------------------------------------------------------------- /CSS-Styling/App.js: -------------------------------------------------------------------------------- 1 | // Good article to read on form https://medium.com/javascript-inside/better-understanding-forms-in-react-a85d889773ce 2 | 3 | import React from 'react' 4 | import './App.css' 5 | 6 | 7 | function ErrorMessage(props) { 8 | const { message } = props 9 | 10 | return {message} 11 | } 12 | 13 | function ControlledInputBox(props) { 14 | const { handleInputChange, textBoxValue, noCityFlag } = props 15 | 16 | return ( 17 | <> 18 | 19 | {noCityFlag && } 20 | 21 | ) 22 | } 23 | 24 | function ButtonComponent(props) { 25 | return ( 26 | 27 | ) 28 | } 29 | 30 | 31 | 32 | class MyComponent extends React.Component { 33 | state = { 34 | cityArray: ['Karachi', 'Lahore', 'Peshawar', 'Quetta'], 35 | textBoxValue: '', 36 | noCityFlag: false, 37 | } 38 | 39 | handleInputChange = (e) => { 40 | const { value } = e.target 41 | this.setState({ 42 | textBoxValue: value 43 | }) 44 | } 45 | 46 | addCity = () => { 47 | const { textBoxValue } = this.state 48 | 49 | this.setState(prevState => ({ 50 | cityArray: [...prevState.cityArray, textBoxValue], 51 | textBoxValue: '', 52 | noCityFlag: false 53 | })) 54 | } 55 | 56 | removeCity = (event) => { 57 | const { cityArray } = this.state 58 | const { value } = event.target 59 | 60 | const newCityArray = cityArray.filter(city => city !== value) 61 | 62 | this.setState({ 63 | cityArray: newCityArray 64 | }) 65 | } 66 | 67 | handleForm = (event) => { 68 | const { textBoxValue } = this.state 69 | 70 | event.preventDefault() 71 | 72 | if (textBoxValue === '') { 73 | this.setState({ noCityFlag: true }) 74 | } else { 75 | this.addCity() 76 | } 77 | } 78 | 79 | render() { 80 | const { cityArray, textBoxValue, noCityFlag } = this.state 81 | 82 | return ( 83 |
84 |
    85 | { 86 | cityArray.map(city =>
  • 5 ? 'red' : 'blue' }}> 87 | {city} 88 | 89 |
  • ) 90 | } 91 |
92 | 93 | 94 | 95 | ) 96 | } 97 | } 98 | 99 | function MyFunctionalComponent(props) { 100 | const { heading } = props 101 | return

{heading}

102 | } 103 | 104 | function App() { 105 | const someCondition = true // try changing true with false 106 | 107 | if (someCondition === true) { 108 | return ( 109 |
110 | 111 | 112 |
113 | ) 114 | } else { 115 | return ( 116 |

Sorry nothing to display

117 | ) 118 | } 119 | } 120 | 121 | export default App 122 | -------------------------------------------------------------------------------- /CSS-Styling/InlineStyling.js: -------------------------------------------------------------------------------- 1 | // Good article to read on form https://medium.com/javascript-inside/better-understanding-forms-in-react-a85d889773ce 2 | 3 | //List of all supported HTML attributes that can be passed along to the React element 4 | //https://facebook.github.io/react/docs/dom-elements.html#all-supported-html-attributes 5 | 6 | import React from 'react' 7 | import './App.css' 8 | 9 | function ErrorMessage (props) { 10 | const { message } = props 11 | 12 | return {message} 13 | } 14 | 15 | function ControlledInputBox (props) { 16 | const { handleInputChange, textBoxValue, noCityFlag } = props 17 | 18 | return ( 19 | <> 20 | 21 | {noCityFlag && } 22 | 23 | ) 24 | } 25 | 26 | function ButtonComponent (props) { 27 | return ( 28 | 31 | ) 32 | } 33 | 34 | class MyComponent extends React.Component { 35 | state = { 36 | cityArray: ['Karachi', 'Lahore', 'Peshawar', 'Quetta'], 37 | textBoxValue: '', 38 | noCityFlag: false 39 | } 40 | 41 | handleInputChange = e => { 42 | const { value } = e.target 43 | this.setState({ 44 | textBoxValue: value 45 | }) 46 | } 47 | 48 | addCity = () => { 49 | const { textBoxValue } = this.state 50 | 51 | this.setState(prevState => ({ 52 | cityArray: [...prevState.cityArray, textBoxValue], 53 | textBoxValue: '', 54 | noCityFlag: false 55 | })) 56 | } 57 | 58 | removeCity = event => { 59 | const { cityArray } = this.state 60 | const { value } = event.target 61 | 62 | const newCityArray = cityArray.filter(city => city !== value) 63 | 64 | this.setState({ 65 | cityArray: newCityArray 66 | }) 67 | } 68 | 69 | handleForm = event => { 70 | const { textBoxValue } = this.state 71 | 72 | event.preventDefault() 73 | 74 | if (textBoxValue === '') { 75 | this.setState({ noCityFlag: true }) 76 | } else { 77 | this.addCity() 78 | } 79 | } 80 | 81 | render () { 82 | const { cityArray, textBoxValue, noCityFlag } = this.state 83 | 84 | return ( 85 |
86 |
    87 | {cityArray.map(city => ( 88 |
  • 5 ? 'red' : 'blue' }} 92 | > 93 | {city} 94 | 101 |
  • 102 | ))} 103 |
104 | 109 | 110 | 111 | ) 112 | } 113 | } 114 | 115 | function MyFunctionalComponent (props) { 116 | const { heading } = props 117 | return

{heading}

118 | } 119 | 120 | function App () { 121 | const someCondition = true // try changing true with false 122 | 123 | if (someCondition === true) { 124 | return ( 125 |
132 |
133 | 134 | 135 |
136 |
137 | ) 138 | } else { 139 | return

Sorry nothing to display

140 | } 141 | } 142 | 143 | export default App 144 | -------------------------------------------------------------------------------- /CSS-Styling/flag.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/CSS-Styling/flag.png -------------------------------------------------------------------------------- /CSS-Styling/imgTagExample.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import styled from 'styled-components' 3 | import logo from './flag.png' 4 | import bgTile from './tile-bg.jpg' 5 | 6 | const Button = styled.button` 7 | background: black; 8 | color: white; 9 | border-radius: 7px; 10 | padding: 20px; 11 | margin: 10px; 12 | font-size: 16px; 13 | :disabled { 14 | opacity: 0.4; 15 | } 16 | :hover { 17 | box-shadow: 0 0 10px yellow; 18 | background-color: white; 19 | color: black; 20 | } 21 | ` 22 | const Div = styled.div` 23 | min-width: 80%; 24 | height: 700px; 25 | display: flex; 26 | justify-content: center; 27 | align-items: center; 28 | border: 1px dotted navy; 29 | background-color: navy; 30 | ` 31 | 32 | const H1 = styled.h1` 33 | color: navy; 34 | font-family: Arial; 35 | ` 36 | 37 | // use styled() for inheritence 38 | const DivNew = styled(Div)` 39 | background-color: lightblue; 40 | background-image: url(${bgTile}); 41 | backgound 42 | ` 43 | 44 | function App () { 45 | return ( 46 |
47 | 48 |

styled-component Example

49 | Logo; 50 | 51 |
52 |
53 | ) 54 | } 55 | 56 | export default App 57 | -------------------------------------------------------------------------------- /CSS-Styling/style-component-example.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import styled from 'styled-components' 3 | 4 | const Button = styled.button` 5 | background: black; 6 | color: white; 7 | border-radius: 7px; 8 | padding: 20px; 9 | margin: 10px; 10 | font-size: 16px; 11 | :disabled { 12 | opacity: 0.4; 13 | } 14 | :hover { 15 | box-shadow: 0 0 10px yellow; 16 | background-color: white; 17 | color: black; 18 | } 19 | ` 20 | const Div = styled.div` 21 | min-width: 80%; 22 | height: 700px; 23 | display: flex; 24 | justify-content: center; 25 | align-items: center; 26 | border: 1px dotted navy; 27 | background-color: navy; 28 | ` 29 | 30 | const H1 = styled.h1` 31 | color: navy; 32 | font-family: Arial; 33 | ` 34 | 35 | // use styled() for inheritence 36 | const DivNew = styled(Div)` 37 | background-color: lightblue; 38 | ` 39 | 40 | function App () { 41 | return ( 42 |
43 | 44 |

styled-component Example

45 | 46 | 47 |
48 |
49 | ) 50 | } 51 | 52 | export default App 53 | -------------------------------------------------------------------------------- /CSS-Styling/tile-bg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/CSS-Styling/tile-bg.jpg -------------------------------------------------------------------------------- /ComponentWithDotFilter.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | class MyComponent extends React.Component { 4 | render () { 5 | const cityArray = ['Karachi', 'Lahore', 'Peshawar', 'Quetta'] 6 | 7 | const shortListedCities = cityArray.filter(city => city.length > 6) 8 | return ( 9 |
    10 | { 11 | shortListedCities.map(city =>
  • {city}
  • ) 12 | } 13 |
14 | ) 15 | } 16 | } 17 | 18 | function App () { 19 | return () 20 | } 21 | 22 | export default App 23 | -------------------------------------------------------------------------------- /ComponentWithDotMap.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | class MyComponent extends React.Component { 4 | render () { 5 | const cityArray = ['Karachi', 'Lahore', 'Peshawar', 'Quetta'] 6 | 7 | return ( 8 |
    9 | { 10 | cityArray.map((city) =>
  • {city}
  • ) 11 | } 12 |
13 | ) 14 | } 15 | } 16 | 17 | function App () { 18 | return () 19 | } 20 | 21 | export default App 22 | -------------------------------------------------------------------------------- /ConditionalRendering.js: -------------------------------------------------------------------------------- 1 | // Good article to read on form https://medium.com/javascript-inside/better-understanding-forms-in-react-a85d889773ce 2 | 3 | import React from 'react' 4 | 5 | 6 | 7 | function ErrorMessage(props) { 8 | const { message } = props 9 | 10 | return {message} 11 | } 12 | 13 | function ControlledInputBox(props) { 14 | const { handleInputChange, textBoxValue, noCityFlag } = props 15 | 16 | return ( 17 | <> 18 | 19 | {noCityFlag && } 20 | 21 | ) 22 | } 23 | 24 | function ButtonComponent(props) { 25 | return ( 26 | 27 | ) 28 | } 29 | 30 | 31 | 32 | class MyComponent extends React.Component { 33 | state = { 34 | cityArray: ['Karachi', 'Lahore', 'Peshawar', 'Quetta'], 35 | textBoxValue: '', 36 | noCityFlag: false, 37 | } 38 | 39 | handleInputChange = (e) => { 40 | const { value } = e.target 41 | this.setState({ 42 | textBoxValue: value 43 | }) 44 | } 45 | 46 | addCity = () => { 47 | const { textBoxValue } = this.state 48 | 49 | this.setState(prevState => ({ 50 | cityArray: [...prevState.cityArray, textBoxValue], 51 | textBoxValue: '', 52 | noCityFlag: false 53 | })) 54 | } 55 | 56 | removeCity = (event) => { 57 | const { cityArray } = this.state 58 | const { value } = event.target 59 | 60 | const newCityArray = cityArray.filter(city => city !== value) 61 | 62 | this.setState({ 63 | cityArray: newCityArray 64 | }) 65 | } 66 | 67 | handleForm = (event) => { 68 | const { textBoxValue } = this.state 69 | 70 | event.preventDefault() 71 | 72 | if (textBoxValue === '') { 73 | this.setState({ noCityFlag: true }) 74 | } else { 75 | this.addCity() 76 | } 77 | } 78 | 79 | render() { 80 | const { cityArray, textBoxValue, noCityFlag } = this.state 81 | 82 | return ( 83 |
84 |
    85 | { 86 | cityArray.map(city =>
  • 5 ? 'red' : 'blue' }}> {city}
  • ) 87 | } 88 |
89 | 90 | 91 | 92 | ) 93 | } 94 | } 95 | 96 | function MyFunctionalComponent(props) { 97 | const { heading } = props 98 | return

{heading}

99 | } 100 | 101 | function App() { 102 | const someCondition = true // try changing true with false 103 | 104 | if (someCondition === true) { 105 | return ( 106 |
107 | 108 | 109 |
110 | ) 111 | } else { 112 | return ( 113 |

Sorry nothing to display

114 | ) 115 | } 116 | } 117 | 118 | export default App 119 | -------------------------------------------------------------------------------- /DestructureStateAndProps.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | class MyComponent extends React.Component { 4 | state = { 5 | cityArray: ['Karachi', 'Lahore', 'Peshawar', 'Quetta'] 6 | } 7 | 8 | render () { 9 | const { myFunction } = this.props 10 | const { cityArray } = this.state 11 | 12 | myFunction() 13 | 14 | return ( 15 |
    16 | { 17 | cityArray.map(city =>
  • {city}
  • ) 18 | } 19 |
20 | ) 21 | } 22 | } 23 | 24 | function MyFunctionalComponent (props) { 25 | const { heading } = props 26 | return

{heading}

27 | } 28 | 29 | function App () { 30 | const myFunction = () => alert('Hello World') 31 | 32 | return ( 33 |
34 | 35 | 36 |
37 | ) 38 | } 39 | 40 | export default App 41 | -------------------------------------------------------------------------------- /DestructuringArray1.js: -------------------------------------------------------------------------------- 1 | // run this file using node DestructuringArray1.js 2 | 3 | const myArray = ['Aamir', 'Pinger', 2] 4 | 5 | const [firstName, lastName, degrees] = myArray 6 | 7 | console.log(firstName) 8 | console.log(lastName) 9 | console.log(degrees) 10 | 11 | const sentence = `${firstName} ${lastName} has ${degrees} Masters degrees.` 12 | 13 | console.log(sentence) 14 | -------------------------------------------------------------------------------- /DestructuringArray2.js: -------------------------------------------------------------------------------- 1 | // run this file using node DestructuringArray2.js 2 | 3 | const myArray = ['Aamir', 'Pinger', 2] 4 | 5 | const [firstName, lastName] = myArray 6 | 7 | console.log(firstName) 8 | console.log(lastName) 9 | console.log(degrees) 10 | 11 | const sentence = `${firstName} ${lastName} has ${degrees} Masters degrees.` 12 | 13 | console.log(sentence) 14 | -------------------------------------------------------------------------------- /DestructuringObject1.js: -------------------------------------------------------------------------------- 1 | // run this file using node DestructuringObject1.js 2 | 3 | const myObject = { firstName: 'Aamir', lastName: 'Pinger', degrees: 2 } 4 | 5 | console.log(myObject.firstName) 6 | console.log(myObject.lastName) 7 | console.log(myObject.degrees) 8 | 9 | const { firstName, lastName, degrees } = myObject 10 | 11 | console.log(firstName) 12 | console.log(lastName) 13 | console.log(degrees) 14 | 15 | const sentence = `${firstName} ${lastName} has ${degrees} Masters degrees` 16 | 17 | console.log(sentence) 18 | -------------------------------------------------------------------------------- /DestructuringObject2.js: -------------------------------------------------------------------------------- 1 | // run this file using node DestructuringObject2.js 2 | 3 | const myObject = { firstName: 'Aamir', lastName: 'Pinger', degrees: 2 } 4 | 5 | console.log(myObject.firstName) 6 | console.log(myObject.lastName) 7 | console.log(myObject.degrees) 8 | 9 | const { firstName, lastName } = myObject 10 | 11 | console.log(firstName) 12 | console.log(lastName) 13 | console.log(degrees) 14 | 15 | const sentence = `${firstName} ${lastName} has ${degrees} Masters degrees` 16 | 17 | console.log(sentence) 18 | -------------------------------------------------------------------------------- /DotFilterExampleCode.js: -------------------------------------------------------------------------------- 1 | // Filter function 2 | 3 | // Example of imparative code 4 | 5 | const names = ['Aamir', 'Ali', 'Javed', 'Aziz', 'Irfan', 'Muneeb'] 6 | const selectedNames = [] 7 | for (let i = 0; i < names.length; i++) { 8 | if (names[i].length >= 5) { 9 | selectedNames.push(names[i]) 10 | } 11 | } 12 | 13 | //* ********************************************* 14 | // run one at a time 15 | 16 | // Example of declarative code using .filter function 17 | 18 | /* 19 | const names = ['Aamir', 'Ali', 'Javed', 'Aziz', 'Irfan', 'Muneeb'] 20 | 21 | const selectedNames = names.filter(name => name.length >= 5) 22 | 23 | */ 24 | -------------------------------------------------------------------------------- /DotMapFunction.js: -------------------------------------------------------------------------------- 1 | //Simple javascript code as an example of imperative code 2 | 3 | const teachers = ['Zia Khan', 'Irfan', 'Muneeb', 'Aamir'] 4 | const titles = [] 5 | 6 | for (let i = 0; i < teachers.length; i++) { 7 | titles[i] = 'Mr. ' + teachers[i] 8 | } 9 | 10 | 11 | 12 | //*********************************************************** 13 | // Run one at a time 14 | 15 | Example of declarative code using simple javascript .map function 16 | 17 | /* 18 | const teachers = ['Zia Khan', 'Irfan', 'Muneeb', 'Aamir'] 19 | 20 | const titles = teachers.map( name => 'Mr. ' + name ) 21 | */ 22 | 23 | -------------------------------------------------------------------------------- /FormExample.js: -------------------------------------------------------------------------------- 1 | // Good article to read on form https://medium.com/javascript-inside/better-understanding-forms-in-react-a85d889773ce 2 | 3 | import React from 'react' 4 | 5 | function ControlledInputBox (props) { 6 | const { handleInputChange, textBoxValue } = props 7 | 8 | return 9 | } 10 | 11 | function ButtonComponent (props) { 12 | return 13 | } 14 | 15 | class MyComponent extends React.Component { 16 | state = { 17 | cityArray: ['Karachi', 'Lahore', 'Peshawar', 'Quetta'], 18 | textBoxValue: '' 19 | } 20 | 21 | handleInputChange = e => { 22 | const { value } = e.target 23 | this.setState({ 24 | textBoxValue: value 25 | }) 26 | } 27 | 28 | addCity = () => { 29 | const { textBoxValue } = this.state 30 | 31 | this.setState(prevState => ({ 32 | cityArray: [...prevState.cityArray, textBoxValue], 33 | textBoxValue: '' 34 | })) 35 | } 36 | 37 | removeCity = event => { 38 | const { cityArray } = this.state 39 | const { value } = event.target 40 | 41 | const newCityArray = cityArray.filter(city => city !== value) 42 | 43 | this.setState({ 44 | cityArray: newCityArray 45 | }) 46 | } 47 | 48 | handleForm = event => { 49 | const { textBoxValue } = this.state 50 | 51 | event.preventDefault() 52 | 53 | if (textBoxValue === '') { 54 | alert('Please enter city name to save') 55 | } else { 56 | this.addCity() 57 | } 58 | } 59 | 60 | render () { 61 | const { cityArray, textBoxValue } = this.state 62 | 63 | return ( 64 |
65 |
    66 | {cityArray.map(city => ( 67 |
  • 68 | {' '} 69 | {city}{' '} 70 | 73 |
  • 74 | ))} 75 |
76 | 80 | 81 | 82 | ) 83 | } 84 | } 85 | 86 | function MyFunctionalComponent (props) { 87 | const { heading } = props 88 | return

{heading}

89 | } 90 | 91 | function App () { 92 | return ( 93 |
94 | 95 | 96 |
97 | ) 98 | } 99 | 100 | export default App 101 | -------------------------------------------------------------------------------- /JSXReactElementExample.js: -------------------------------------------------------------------------------- 1 | // JSX in React Example code 2 | 3 | 4 | import React from 'react'; 5 | import ReactDOM from 'react-dom'; 6 | 7 | const element =

Hello World

8 | 9 | console.log(element) 10 | 11 | ReactDOM.render( 12 | element, 13 | document.getElementById('root') 14 | ) 15 | 16 | 17 | //**************************************************** 18 | // run one at a time 19 | 20 | /* 21 | import React from 'react'; 22 | import ReactDOM from 'react-dom'; 23 | 24 | const cityArray = ["Karachi", "Lahore", "Peshawer", "Quetta"] 25 | 26 | const myElement =
    27 | { 28 | cityArray.map((city) =>
  • {city}
  • ) 29 | } 30 |
31 | 32 | ReactDOM.render( 33 | myElement, 34 | document.getElementById('root') 35 | ); 36 | 37 | 38 | */ 39 | -------------------------------------------------------------------------------- /PassingProps.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | class MyComponent extends React.Component { 4 | render () { 5 | const cityArray = this.props.cityArray 6 | 7 | const shortListedCities = cityArray.filter(city => city.length > 6) 8 | 9 | return ( 10 |
    11 | {shortListedCities.map(city =>
  • {city}
  • )} 12 |
13 | ) 14 | } 15 | } 16 | 17 | function App () { 18 | const cityArray1 = ['Karachi', 'Lahore', 'Peshawar', 'Quetta'] 19 | const cityArray2 = ['Hyderabad', 'Islamabad', 'Sawat', 'Gawader'] 20 | 21 | return ( 22 |
23 | 24 | 25 |
26 | ) 27 | } 28 | 29 | export default App 30 | -------------------------------------------------------------------------------- /PassingPropsAndFunction.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | class MyComponent extends React.Component { 4 | render () { 5 | const cityArray = this.props.cityArray 6 | 7 | const shortListedCities = cityArray.filter(city => city.length > 6) 8 | 9 | this.props.myFunction() 10 | 11 | return ( 12 |
    13 | {shortListedCities.map(city =>
  • {city}
  • )} 14 |
15 | ) 16 | } 17 | } 18 | 19 | function App () { 20 | const cityArray1 = ['Karachi', 'Lahore', 'Peshawar', 'Quetta'] 21 | const cityArray2 = ['Hyderabad', 'Islamabad', 'Sawat', 'Gawader'] 22 | 23 | const myFunction = () => alert('Hello World') 24 | 25 | return ( 26 |
27 | 28 | 29 |
30 | ) 31 | } 32 | 33 | export default App 34 | -------------------------------------------------------------------------------- /PassingPropsToComponentExample.js: -------------------------------------------------------------------------------- 1 | // App.js file 2 | 3 | import React, { Component, Fragment } from 'react'; 4 | 5 | class App extends Component { 6 | render() { 7 | return ( 8 | //You can also use
instead of 9 | 10 | 11 | 12 | 13 | ) 14 | } 15 | } 16 | 17 | class City extends Component { 18 | 19 | render() { 20 | const cityArray = this.props.cityArray 21 | // you can also do like this 22 | //const { cityArray } = this.props 23 | 24 | return ( 25 | //You can also use
instead of 26 | 27 |
    28 | { 29 | cityArray.map((city) =>
  • {city}
  • ) 30 | } 31 |
32 |

{this.props.name}

33 |
34 | ) 35 | } 36 | } 37 | 38 | export default App; 39 | -------------------------------------------------------------------------------- /PropTypeExample.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import PropType from 'prop-types' 3 | 4 | class MyComponent extends React.Component { 5 | render () { 6 | const cityArray = this.props.cityArray 7 | 8 | const shortListedCities = cityArray.filter(city => city.length > 6) 9 | 10 | // this.props.myFunction() 11 | 12 | return ( 13 |
    14 | {shortListedCities.map(city =>
  • {city}
  • )} 15 |
16 | ) 17 | } 18 | } 19 | 20 | MyComponent.propTypes = { 21 | cityArray: PropType.array.isRequired 22 | } 23 | 24 | function MyFunctionalComponent (props) { 25 | return

{props.heading}

26 | } 27 | 28 | function App () { 29 | const cityArray1 = ['Karachi', 'Lahore', 'Peshawar', 'Quetta'] 30 | const cityArray2 = ['Hyderabad', 'Islamabad', 'Sawat', 'Gawader'] 31 | 32 | const cityObj = { city1: 'Karachi', city2: 'Lahore', city3: 'Peshawar', city4: 'Quetta' } 33 | 34 | const myFunction = () => alert('Hello World') 35 | 36 | return ( 37 |
38 | 39 | 40 | 41 |
42 | ) 43 | } 44 | 45 | export default App 46 | -------------------------------------------------------------------------------- /PropTypeInClass.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import PropType from 'prop-types' 3 | 4 | class MyComponent extends React.Component { 5 | static propTypes = { 6 | cityArray: PropType.array.isRequired 7 | } 8 | 9 | render () { 10 | const cityArray = this.props.cityArray 11 | 12 | const shortListedCities = cityArray.filter(city => city.length > 6) 13 | 14 | // this.props.myFunction() 15 | 16 | return ( 17 |
    18 | {shortListedCities.map(city =>
  • {city}
  • )} 19 |
20 | ) 21 | } 22 | } 23 | 24 | function MyFunctionalComponent (props) { 25 | return

{props.heading}

26 | } 27 | 28 | function App () { 29 | const cityArray1 = ['Karachi', 'Lahore', 'Peshawar', 'Quetta'] 30 | const cityArray2 = ['Hyderabad', 'Islamabad', 'Sawat', 'Gawader'] 31 | 32 | const cityObj = { city1: 'Karachi', city2: 'Lahore', city3: 'Peshawar', city4: 'Quetta' } 33 | 34 | const myFunction = () => alert('Hello World') 35 | 36 | return ( 37 |
38 | 39 | 40 | 41 |
42 | ) 43 | } 44 | 45 | export default App 46 | -------------------------------------------------------------------------------- /PropsToFunctionalComp.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | class MyComponent extends React.Component { 4 | render () { 5 | const cityArray = this.props.cityArray 6 | 7 | const shortListedCities = cityArray.filter(city => city.length > 6) 8 | 9 | // this.props.myFunction() 10 | 11 | return ( 12 |
    13 | {shortListedCities.map(city =>
  • {city}
  • )} 14 |
15 | ) 16 | } 17 | } 18 | 19 | function MyFunctionalComponent (props) { 20 | return

{props.heading}

21 | } 22 | 23 | function App () { 24 | const cityArray1 = ['Karachi', 'Lahore', 'Peshawar', 'Quetta'] 25 | const cityArray2 = ['Hyderabad', 'Islamabad', 'Sawat', 'Gawader'] 26 | 27 | const myFunction = () => alert('Hello World') 28 | 29 | return ( 30 |
31 | 32 | 33 | 34 |
35 | ) 36 | } 37 | 38 | export default App 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React Fundamental Slides Code 2 | 3 | ### This repository contain all the example code shown during the React fundamental training 4 | 5 | 6 | For online practice of React https://codesandbox.io 7 | 8 | 9 | Some of the DOM attributes supported by React are listed on following link. 10 | https://reactjs.org/docs/dom-elements.html#all-supported-html-attributes 11 | 12 | Resourse: 13 | 1) The Road to learn react (Free book) https://roadtoreact.com/ 14 | 2) 30-days-of-react - https://www.fullstackreact.com/30-days-of-react/ 15 | 16 | 17 | 18 | ### Things to install for React environment 19 | 20 | 1) Node.js and npm. you can download and install from https://www.npmjs.com/get-npm 21 | 22 | 2) Once node and npm is installed on your system run following command to install create-react-app to setup React enviorment : 23 | #### npm install -g create-react-app 24 | 25 | 26 | ### To initiate react project 27 | 28 | #### $ npx create-react-app < project-name > 29 | 30 | #### $ cd < project-name > 31 | 32 | #### $ npm start / yarn start 33 | -------------------------------------------------------------------------------- /RemoveCityFromState.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | function ControlledInputBox (props) { 4 | const { handleInputChange, textBoxValue } = props 5 | 6 | return ( 7 | 8 | ) 9 | } 10 | 11 | function ButtonComponent (props) { 12 | const { addCity } = props 13 | return ( 14 | <> 15 | 16 | 17 | ) 18 | } 19 | 20 | class MyComponent extends React.Component { 21 | state = { 22 | cityArray: ['Karachi', 'Lahore', 'Peshawar', 'Quetta'], 23 | textBoxValue: '' 24 | } 25 | 26 | handleInputChange = (e) => { 27 | const { value } = e.target 28 | this.setState({ 29 | textBoxValue: value 30 | }) 31 | } 32 | 33 | addCity = () => { 34 | const { textBoxValue } = this.state 35 | 36 | this.setState(prevState => ({ 37 | cityArray: [...prevState.cityArray, textBoxValue], 38 | textBoxValue: '' 39 | })) 40 | } 41 | 42 | removeCity = (event) => { 43 | const { cityArray } = this.state 44 | const { value } = event.target 45 | 46 | const newCityArray = cityArray.filter(city => city !== value) 47 | 48 | this.setState({ 49 | cityArray: newCityArray 50 | }) 51 | } 52 | 53 | render () { 54 | const { cityArray, textBoxValue } = this.state 55 | 56 | return ( 57 | <> 58 |
    59 | { 60 | cityArray.map(city =>
  • {city}
  • ) 61 | } 62 |
63 | 64 | this.addCity()}/> 65 | 66 | ) 67 | } 68 | } 69 | 70 | function MyFunctionalComponent (props) { 71 | const { heading } = props 72 | return

{heading}

73 | } 74 | 75 | function App () { 76 | return ( 77 |
78 | 79 | 80 |
81 | ) 82 | } 83 | 84 | export default App 85 | -------------------------------------------------------------------------------- /RouterExamples/Index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import * as serviceWorker from './serviceWorker'; 6 | 7 | import { BrowserRouter } from 'react-router-dom' 8 | 9 | ReactDOM.render( 10 | 11 | 12 | , document.getElementById('root')); 13 | 14 | 15 | -------------------------------------------------------------------------------- /RouterExamples/SwitchWithRouter.js: -------------------------------------------------------------------------------- 1 | // ************************************************************************************************ 2 | // This example illustrate how to use component with react route components 3 | // 4 | // App.js file 5 | // 6 | // ************************************************************************************************* 7 | 8 | import React, { Component, Fragment } from 'react' 9 | import { Link, Route, Switch } from 'react-router-dom' 10 | 11 | function ComponentHw(props) { 12 | return

{`HELLO WORLD ${props.text}`}

13 | } 14 | 15 | function ComponentSlogan() { 16 | return

PAKISTAN ZINDABAD

17 | } 18 | 19 | function ErrorComponent() { 20 | return

404 - ERROR

21 | } 22 | class App extends Component { 23 | render() { 24 | return ( 25 | 26 | 27 | 28 | { 29 | //to check switch functionality try typing wrong route in 30 | // browser URL for example: http://localhost:3000/pakistan 31 | } 32 | 33 | 34 | 35 | } /> 36 | 37 | 38 | 39 | Goto to Hello World Page 40 | 41 | ) 42 | } 43 | } 44 | 45 | export default App 46 | -------------------------------------------------------------------------------- /RouterExamples/URLParametersExample.js: -------------------------------------------------------------------------------- 1 | 2 | // ************************************************************************** 3 | // 4 | // App.js file 5 | // 6 | // ************************************************************************** 7 | 8 | import React, { Component, Fragment } from 'react' 9 | import { Link, Route, Switch, Redirect, withRouter } from 'react-router-dom' 10 | 11 | function ErrorComponent() { 12 | return

404 - ERROR

13 | } 14 | 15 | function LoginComponent(props) { 16 | console.log(props) 17 | return

18 | {`User Login Page - the props value received in 19 | variable humanType is : ${props.location.state.humanType} 20 | and the value of variable age received as a router props is : ${props.location.state.age} 21 | `} 22 |

23 | } 24 | 25 | function ComponentHw(props) { 26 | console.log(props) 27 | return

HELLO WORLD - {props.match.params.extraText}

28 | } 29 | 30 | ComponentHw = withRouter(ComponentHw) 31 | 32 | class ComponentSlogan extends Component { 33 | state = { 34 | userLogin: true 35 | } 36 | 37 | componentDidMount() { 38 | setTimeout(() => { 39 | this.setState((ps) => ({ 40 | userLogin: !ps.userLogin, 41 | })) 42 | }, 2000) 43 | } 44 | 45 | render() { 46 | return (this.state.userLogin) 47 | ?

PAKISTAN ZINDABAD

48 | : 57 | } 58 | } 59 | 60 | class App extends Component { 61 | 62 | 63 | render() { 64 | const name = "Aamir" 65 | return ( 66 | 67 | 68 | 69 | } /> 70 | 71 | 72 | 73 | Goto to Hello World Page 74 | 75 | 76 | 77 | ) 78 | } 79 | } 80 | 81 | export default App 82 | -------------------------------------------------------------------------------- /RouterExamples/propsWithRedirectExample.js: -------------------------------------------------------------------------------- 1 | // ************************************************************************** 2 | // 3 | // App.js file 4 | // 5 | // ************************************************************************** 6 | 7 | import React, { Component, Fragment } from 'react' 8 | import { Link, Route, Switch, Redirect } from 'react-router-dom' 9 | 10 | function ErrorComponent() { 11 | return

404 - ERROR

12 | } 13 | 14 | function LoginComponent(props) { 15 | console.log(props) 16 | return

17 | {`User Login Page - the props value received in 18 | variable humanType is : ${props.location.state.humanType} 19 | and the value of variable age received as a router props is : ${props.location.state.age} 20 | `} 21 |

22 | } 23 | 24 | function ComponentHw() { 25 | return

HELLO WORLD

26 | } 27 | 28 | class ComponentSlogan extends Component { 29 | state = { 30 | userLogin: true 31 | } 32 | 33 | componentDidMount() { 34 | setTimeout(() => { 35 | this.setState((ps) => ({ 36 | userLogin: !ps.userLogin, 37 | })) 38 | }, 2000) 39 | } 40 | 41 | render() { 42 | return (this.state.userLogin) 43 | ?

PAKISTAN ZINDABAD

44 | : 53 | } 54 | } 55 | 56 | class App extends Component { 57 | 58 | 59 | render() { 60 | return ( 61 | 62 | 63 | 64 | } /> 65 | 66 | 67 | 68 | Goto to Hello World Page 69 | 70 | 71 | 72 | ) 73 | } 74 | } 75 | 76 | export default App 77 | -------------------------------------------------------------------------------- /RouterExamples/redirectRouterComponent.js: -------------------------------------------------------------------------------- 1 | // ************************************************************************************************ 2 | // This example illustrate how to programmatically redirect from one route to another in react 3 | // 4 | // App.js file 5 | // 6 | // ************************************************************************************************* 7 | 8 | import React, { Component, Fragment } from 'react' 9 | import { Link, Route, Switch, Redirect } from 'react-router-dom' 10 | 11 | function ErrorComponent() { 12 | return

404 - ERROR

13 | } 14 | 15 | function LoginComponent() { 16 | return

User Login Page

17 | } 18 | 19 | function ComponentHw() { 20 | return

HELLO WORLD

21 | } 22 | 23 | class ComponentSlogan extends Component { 24 | state = { 25 | userLogin: true 26 | } 27 | 28 | componentDidMount() { 29 | setTimeout(() => { 30 | this.setState((ps) => ({ 31 | userLogin: !ps.userLogin, 32 | })) 33 | }, 2000) 34 | } 35 | 36 | render() { 37 | return (this.state.userLogin) 38 | ?

PAKISTAN ZINDABAD

39 | : 40 | } 41 | } 42 | 43 | class App extends Component { 44 | 45 | 46 | render() { 47 | return ( 48 | 49 | 50 | } /> 51 | } /> 52 | 53 | 54 | 55 | Goto to Hello World Page 56 | 57 | 58 | 59 | ) 60 | } 61 | } 62 | 63 | export default App 64 | -------------------------------------------------------------------------------- /RouterExamples/simpleRoute.js: -------------------------------------------------------------------------------- 1 | // ************************************************************************************************ 2 | // This example illustrate how add simple routes in the react app 3 | // 4 | // App.js file 5 | // 6 | // ************************************************************************************************* 7 | 8 | import React, { Component, Fragment } from 'react' 9 | import { Link, Route } from 'react-router-dom' 10 | 11 | function ComponentHw(props) { 12 | return

{`HELLO WORLD ${props.text}`}

13 | } 14 | 15 | function ComponentSlogan() { 16 | return

PAKISTAN ZINDABAD

17 | } 18 | 19 | 20 | class App extends Component { 21 | render() { 22 | return ( 23 | 24 | 25 | 26 | } /> 27 | 28 | 29 | Goto to Hello World Page 30 | 31 | ) 32 | } 33 | } 34 | 35 | export default App 36 | 37 | -------------------------------------------------------------------------------- /RoutesExample/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /RoutesExample/README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `yarn start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `yarn test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `yarn build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `yarn eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | 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. 35 | 36 | 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. 37 | 38 | 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. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | 46 | ### Code Splitting 47 | 48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 49 | 50 | ### Analyzing the Bundle Size 51 | 52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 53 | 54 | ### Making a Progressive Web App 55 | 56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 57 | 58 | ### Advanced Configuration 59 | 60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 61 | 62 | ### Deployment 63 | 64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 65 | 66 | ### `yarn build` fails to minify 67 | 68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 69 | -------------------------------------------------------------------------------- /RoutesExample/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "my-app", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^4.2.4", 7 | "@testing-library/react": "^9.3.2", 8 | "@testing-library/user-event": "^7.1.2", 9 | "prop-types": "^15.7.2", 10 | "react": "^16.13.0", 11 | "react-dom": "^16.13.0", 12 | "react-router-dom": "^5.1.2", 13 | "react-scripts": "3.4.0", 14 | "styled-components": "^5.0.1" 15 | }, 16 | "scripts": { 17 | "start": "react-scripts start", 18 | "build": "react-scripts build", 19 | "test": "react-scripts test", 20 | "eject": "react-scripts eject" 21 | }, 22 | "eslintConfig": { 23 | "extends": "react-app" 24 | }, 25 | "browserslist": { 26 | "production": [ 27 | ">0.2%", 28 | "not dead", 29 | "not op_mini all" 30 | ], 31 | "development": [ 32 | "last 1 chrome version", 33 | "last 1 firefox version", 34 | "last 1 safari version" 35 | ] 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /RoutesExample/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/RoutesExample/public/favicon.ico -------------------------------------------------------------------------------- /RoutesExample/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /RoutesExample/public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/RoutesExample/public/logo192.png -------------------------------------------------------------------------------- /RoutesExample/public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/RoutesExample/public/logo512.png -------------------------------------------------------------------------------- /RoutesExample/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /RoutesExample/public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /RoutesExample/src/Components/AboutUs/AboutUs.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | export default function AboutUs () { 4 | return

About Us Page

5 | } 6 | -------------------------------------------------------------------------------- /RoutesExample/src/Components/App.css: -------------------------------------------------------------------------------- 1 | h1 { 2 | color: navy; 3 | } 4 | 5 | .listItem { 6 | font-size: 16px; 7 | margin: 10px; 8 | font-family: 'Times New Roman', Times, serif; 9 | } 10 | -------------------------------------------------------------------------------- /RoutesExample/src/Components/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import ErrorMessage from './ErrorMessage/ErrorMessage' 3 | import Routes from '../Routes' 4 | import { Link } from 'react-router-dom' 5 | 6 | import './App.css' 7 | 8 | function App () { 9 | const showSite = true 10 | 11 | if (showSite) { 12 | return ( 13 |
14 | 15 | Goto to Home Page 16 |
17 | Goto to About Us Page 18 |
19 | ) 20 | } else { 21 | return 22 | } 23 | } 24 | 25 | export default App 26 | -------------------------------------------------------------------------------- /RoutesExample/src/Components/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render } from '@testing-library/react'; 3 | import App from './App'; 4 | 5 | test('renders learn react link', () => { 6 | const { getByText } = render(); 7 | const linkElement = getByText(/learn react/i); 8 | expect(linkElement).toBeInTheDocument(); 9 | }); 10 | -------------------------------------------------------------------------------- /RoutesExample/src/Components/ButtonComponent/ButtonComponent.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import styled from 'styled-components' 3 | 4 | export const Button = styled.button` 5 | background-color: white; 6 | color: navy; 7 | width: 80px; 8 | height: 40px;c 9 | ` 10 | export default function ButtonComponent () { 11 | return ( 12 | <> 13 | 14 | 15 | ) 16 | } 17 | -------------------------------------------------------------------------------- /RoutesExample/src/Components/CityList/CityList.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import MyComponent from '../MyComponent/MyComponent' 3 | import MyFunctionalComponent from '../MyFunctionalComponent/MyFunctionalComponent' 4 | 5 | export default function CityList (props) { 6 | const { city } = props.match.params 7 | return ( 8 | <> 9 | 10 | 11 | 12 | ) 13 | } 14 | -------------------------------------------------------------------------------- /RoutesExample/src/Components/ControlledInputBox/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import ErrorMessage from '../ErrorMessage/ErrorMessage' 3 | 4 | export default function ControlledInputBox (props) { 5 | const { handleInputChange, textBoxValue, errorShowFlag } = props 6 | 7 | return ( 8 | <> 9 | 10 | {errorShowFlag && } 11 | 12 | ) 13 | } 14 | -------------------------------------------------------------------------------- /RoutesExample/src/Components/ErrorMessage/ErrorMessage.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | export default function ErrorMessage (props) { 4 | const { errorMsg } = props 5 | 6 | return {errorMsg} 7 | } 8 | -------------------------------------------------------------------------------- /RoutesExample/src/Components/MyComponent/MyComponent.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react' 2 | import styled from 'styled-components' 3 | import ButtonComponent, { Button } from '../ButtonComponent/ButtonComponent' 4 | import ErrorMessage from '../ErrorMessage/ErrorMessage' 5 | import ControlledInputBox from '../ControlledInputBox' 6 | 7 | const Button2 = styled(Button)` 8 | background-color: silver; 9 | ` 10 | 11 | class MyComponent extends Component { 12 | state = { 13 | cityArray: [], 14 | textBoxValue: '', 15 | errorShowFlag: false 16 | } 17 | 18 | componentDidMount () { 19 | /** External API call can be done here and update the state when data received */ 20 | const dataReceivedFromAPICall = [ 21 | 'Karachi', 22 | 'Lahore', 23 | 'Peshawar', 24 | 'Quetta', 25 | 'Swat' 26 | ] 27 | 28 | setTimeout(() => { 29 | this.setState({ 30 | cityArray: dataReceivedFromAPICall 31 | }) 32 | }, 1000) 33 | } 34 | 35 | componentDidUpdate (prevProps, prevState) { 36 | const cityArrayPrev = prevState.cityArray 37 | const cityArrayNew = this.state.cityArray 38 | 39 | if (cityArrayPrev !== cityArrayNew) { 40 | console.log('State updated with new data', cityArrayNew) 41 | } 42 | } 43 | 44 | handleInputChange = e => { 45 | const { value } = e.target 46 | this.setState({ 47 | textBoxValue: value, 48 | errorShowFlag: false 49 | }) 50 | } 51 | 52 | addCity = () => { 53 | const { textBoxValue } = this.state 54 | 55 | this.setState(prevState => ({ 56 | cityArray: [...prevState.cityArray, textBoxValue], 57 | textBoxValue: '' 58 | })) 59 | } 60 | 61 | removeCity = event => { 62 | const { cityArray } = this.state 63 | const { value } = event.target 64 | 65 | const newCityArray = cityArray.filter(city => city !== value) 66 | 67 | this.setState({ 68 | cityArray: newCityArray 69 | }) 70 | } 71 | 72 | handleSubmit = event => { 73 | event.preventDefault() 74 | const { textBoxValue } = this.state 75 | 76 | if (textBoxValue === '') { 77 | this.setState({ 78 | errorShowFlag: true 79 | }) 80 | } else { 81 | this.addCity() 82 | } 83 | } 84 | 85 | render () { 86 | const { cityArray, textBoxValue, errorShowFlag } = this.state 87 | const showSite = cityArray.length !== 0 88 | 89 | return !showSite ? ( 90 | 91 | ) : ( 92 | <> 93 |
    94 | {cityArray.map(city => ( 95 |
  • 100 | {city} 101 | 102 | X 103 | 104 |
  • 105 | ))} 106 |
107 |
108 | 113 | 114 | 115 | 116 | ) 117 | } 118 | } 119 | 120 | export default MyComponent 121 | -------------------------------------------------------------------------------- /RoutesExample/src/Components/MyFunctionalComponent/MyFunctionalComponent.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import styled from 'styled-components' 3 | 4 | const H1 = styled.h1` 5 | font-size: 36px; 6 | color: Olive; 7 | ` 8 | 9 | export default function MyFunctionalComponent (props) { 10 | const { heading } = props 11 | return

{heading}

12 | } 13 | -------------------------------------------------------------------------------- /RoutesExample/src/Routes/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import CityList from '../Components/CityList/CityList' 3 | import AboutUs from '../Components/AboutUs/AboutUs' 4 | 5 | import { Route, Switch } from 'react-router-dom' 6 | 7 | function Routes () { 8 | return ( 9 | 10 | 11 | 12 | 13 | 14 | ) 15 | } 16 | 17 | export default Routes 18 | -------------------------------------------------------------------------------- /RoutesExample/src/Static/Images/flag.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/RoutesExample/src/Static/Images/flag.png -------------------------------------------------------------------------------- /RoutesExample/src/Static/Images/tile-bg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/RoutesExample/src/Static/Images/tile-bg.jpg -------------------------------------------------------------------------------- /RoutesExample/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /RoutesExample/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import ReactDOM from 'react-dom' 3 | import './index.css' 4 | import App from './Components/App' 5 | import * as serviceWorker from './serviceWorker' 6 | 7 | import { BrowserRouter } from 'react-router-dom' 8 | 9 | ReactDOM.render( 10 | 11 | 12 | , 13 | document.getElementById('root') 14 | ) 15 | 16 | // If you want your app to work offline and load faster, you can change 17 | // unregister() to register() below. Note this comes with some pitfalls. 18 | // Learn more about service workers: https://bit.ly/CRA-PWA 19 | serviceWorker.unregister() 20 | -------------------------------------------------------------------------------- /RoutesExample/src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom/extend-expect'; 6 | -------------------------------------------------------------------------------- /SampleFolderStructure/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /SampleFolderStructure/README.md: -------------------------------------------------------------------------------- 1 | # To run this project 2 | 3 | ``` 4 | npm install 5 | ``` 6 | 7 | ``` 8 | npm start 9 | ``` 10 | -------------------------------------------------------------------------------- /SampleFolderStructure/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "my-app", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^4.2.4", 7 | "@testing-library/react": "^9.3.2", 8 | "@testing-library/user-event": "^7.1.2", 9 | "prop-types": "^15.7.2", 10 | "react": "^16.13.0", 11 | "react-dom": "^16.13.0", 12 | "react-router-dom": "^5.1.2", 13 | "react-scripts": "3.4.0", 14 | "styled-components": "^5.0.1" 15 | }, 16 | "scripts": { 17 | "start": "react-scripts start", 18 | "build": "react-scripts build", 19 | "test": "react-scripts test", 20 | "eject": "react-scripts eject" 21 | }, 22 | "eslintConfig": { 23 | "extends": "react-app" 24 | }, 25 | "browserslist": { 26 | "production": [ 27 | ">0.2%", 28 | "not dead", 29 | "not op_mini all" 30 | ], 31 | "development": [ 32 | "last 1 chrome version", 33 | "last 1 firefox version", 34 | "last 1 safari version" 35 | ] 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /SampleFolderStructure/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/SampleFolderStructure/public/favicon.ico -------------------------------------------------------------------------------- /SampleFolderStructure/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /SampleFolderStructure/public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/SampleFolderStructure/public/logo192.png -------------------------------------------------------------------------------- /SampleFolderStructure/public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/SampleFolderStructure/public/logo512.png -------------------------------------------------------------------------------- /SampleFolderStructure/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /SampleFolderStructure/public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /SampleFolderStructure/src/Components/App.css: -------------------------------------------------------------------------------- 1 | h1 { 2 | color: navy; 3 | } 4 | 5 | .listItem { 6 | font-size: 16px; 7 | margin: 10px; 8 | font-family: 'Times New Roman', Times, serif; 9 | } 10 | -------------------------------------------------------------------------------- /SampleFolderStructure/src/Components/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import MyFunctionalComponent from './MyFunctionalComponent/MyFunctionalComponent' 3 | import MyComponent from './MyComponent/MyComponent' 4 | import ErrorMessage from './ErrorMessage/ErrorMessage' 5 | 6 | import './App.css' 7 | 8 | function App () { 9 | const showSite = true 10 | 11 | if (showSite) { 12 | return ( 13 |
14 | 15 | 16 |
17 | ) 18 | } else { 19 | return 20 | } 21 | } 22 | 23 | export default App 24 | -------------------------------------------------------------------------------- /SampleFolderStructure/src/Components/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render } from '@testing-library/react'; 3 | import App from './App'; 4 | 5 | test('renders learn react link', () => { 6 | const { getByText } = render(); 7 | const linkElement = getByText(/learn react/i); 8 | expect(linkElement).toBeInTheDocument(); 9 | }); 10 | -------------------------------------------------------------------------------- /SampleFolderStructure/src/Components/ButtonComponent/ButtonComponent.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import styled from 'styled-components' 3 | 4 | export const Button = styled.button` 5 | background-color: white; 6 | color: navy; 7 | width: 80px; 8 | height: 40px;c 9 | ` 10 | export default function ButtonComponent () { 11 | return ( 12 | <> 13 | 14 | 15 | ) 16 | } 17 | -------------------------------------------------------------------------------- /SampleFolderStructure/src/Components/ControlledInputBox/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import ErrorMessage from '../ErrorMessage/ErrorMessage' 3 | 4 | export default function ControlledInputBox (props) { 5 | const { handleInputChange, textBoxValue, errorShowFlag } = props 6 | 7 | return ( 8 | <> 9 | 10 | {errorShowFlag && } 11 | 12 | ) 13 | } 14 | -------------------------------------------------------------------------------- /SampleFolderStructure/src/Components/ErrorMessage/ErrorMessage.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | export default function ErrorMessage (props) { 4 | const { errorMsg } = props 5 | 6 | return {errorMsg} 7 | } 8 | -------------------------------------------------------------------------------- /SampleFolderStructure/src/Components/MyComponent/MyComponent.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react' 2 | import styled from 'styled-components' 3 | import ButtonComponent, { Button } from '../ButtonComponent/ButtonComponent' 4 | import ErrorMessage from '../ErrorMessage/ErrorMessage' 5 | import ControlledInputBox from '../ControlledInputBox' 6 | 7 | const Button2 = styled(Button)` 8 | background-color: silver; 9 | ` 10 | 11 | class MyComponent extends Component { 12 | state = { 13 | cityArray: [], 14 | textBoxValue: '', 15 | errorShowFlag: false 16 | } 17 | 18 | componentDidMount () { 19 | /** External API call can be done here and update the state when data received */ 20 | const dataReceivedFromAPICall = [ 21 | 'Karachi', 22 | 'Lahore', 23 | 'Peshawar', 24 | 'Quetta', 25 | 'Swat' 26 | ] 27 | 28 | setTimeout(() => { 29 | this.setState({ 30 | cityArray: dataReceivedFromAPICall 31 | }) 32 | }, 1000) 33 | } 34 | 35 | componentDidUpdate (prevProps, prevState) { 36 | const cityArrayPrev = prevState.cityArray 37 | const cityArrayNew = this.state.cityArray 38 | 39 | if (cityArrayPrev !== cityArrayNew) { 40 | console.log('State updated with new data', cityArrayNew) 41 | } 42 | } 43 | 44 | handleInputChange = e => { 45 | const { value } = e.target 46 | this.setState({ 47 | textBoxValue: value, 48 | errorShowFlag: false 49 | }) 50 | } 51 | 52 | addCity = () => { 53 | const { textBoxValue } = this.state 54 | 55 | this.setState(prevState => ({ 56 | cityArray: [...prevState.cityArray, textBoxValue], 57 | textBoxValue: '' 58 | })) 59 | } 60 | 61 | removeCity = event => { 62 | const { cityArray } = this.state 63 | const { value } = event.target 64 | 65 | const newCityArray = cityArray.filter(city => city !== value) 66 | 67 | this.setState({ 68 | cityArray: newCityArray 69 | }) 70 | } 71 | 72 | handleSubmit = event => { 73 | event.preventDefault() 74 | const { textBoxValue } = this.state 75 | 76 | if (textBoxValue === '') { 77 | this.setState({ 78 | errorShowFlag: true 79 | }) 80 | } else { 81 | this.addCity() 82 | } 83 | } 84 | 85 | render () { 86 | const { cityArray, textBoxValue, errorShowFlag } = this.state 87 | const showSite = cityArray.length !== 0 88 | 89 | return !showSite ? ( 90 | 91 | ) : ( 92 | <> 93 |
    94 | {cityArray.map(city => ( 95 |
  • 100 | {city} 101 | 102 | X 103 | 104 |
  • 105 | ))} 106 |
107 |
108 | 113 | 114 | 115 | 116 | ) 117 | } 118 | } 119 | 120 | export default MyComponent 121 | -------------------------------------------------------------------------------- /SampleFolderStructure/src/Components/MyFunctionalComponent/MyFunctionalComponent.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import styled from 'styled-components' 3 | 4 | const H1 = styled.h1` 5 | font-size: 36px; 6 | color: Olive; 7 | ` 8 | 9 | export default function MyFunctionalComponent (props) { 10 | const { heading } = props 11 | return

{heading}

12 | } 13 | -------------------------------------------------------------------------------- /SampleFolderStructure/src/Static/Images/flag.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/SampleFolderStructure/src/Static/Images/flag.png -------------------------------------------------------------------------------- /SampleFolderStructure/src/Static/Images/tile-bg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aamirpinger/react-fundamental-slides-code/4145a7496073b70124b6b73e85c4a4d5d631f21c/SampleFolderStructure/src/Static/Images/tile-bg.jpg -------------------------------------------------------------------------------- /SampleFolderStructure/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /SampleFolderStructure/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import ReactDOM from 'react-dom' 3 | import './index.css' 4 | import App from './Components/App' 5 | import * as serviceWorker from './serviceWorker' 6 | 7 | ReactDOM.render(, document.getElementById('root')) 8 | 9 | // If you want your app to work offline and load faster, you can change 10 | // unregister() to register() below. Note this comes with some pitfalls. 11 | // Learn more about service workers: https://bit.ly/CRA-PWA 12 | serviceWorker.unregister() 13 | -------------------------------------------------------------------------------- /SampleFolderStructure/src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom/extend-expect'; 6 | -------------------------------------------------------------------------------- /StateExample.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import PropType from 'prop-types' 3 | 4 | class MyComponent extends React.Component { 5 | state = { 6 | cityArray: ['Karachi', 'Lahore', 'Peshawar', 'Quetta'] 7 | } 8 | 9 | render () { 10 | return ( 11 |
    12 | { 13 | this.state.cityArray.map(city =>
  • {city}
  • ) 14 | } 15 |
16 | ) 17 | } 18 | } 19 | 20 | function App () { 21 | return ( 22 |
23 | 24 |
25 | ) 26 | } 27 | 28 | export default App 29 | -------------------------------------------------------------------------------- /StateExampleCode.js: -------------------------------------------------------------------------------- 1 | import React, { Component, Fragment } from 'react'; 2 | 3 | class App extends Component { 4 | state = { 5 | cityArray1: ["Karachi", "Lahore", "Peshawer", "Quetta"], 6 | cityArray2: ["Hyderabad", "Multan", "Gawader", "Mardan"] 7 | } 8 | render() { 9 | return ( 10 | //You can also use
instead of 11 | 12 | 13 | 14 | 15 | ) 16 | } 17 | } 18 | 19 | class City extends Component { 20 | 21 | render() { 22 | const cityArray = this.props.cityArray 23 | // you can also do like this 24 | //const { cityArray } = this.props 25 | 26 | return ( 27 | //You can also use
instead of 28 | 29 |
    30 | { 31 | cityArray.map((city) =>
  • {city}
  • ) 32 | } 33 |
34 |

{this.props.name}

35 |
36 | ) 37 | } 38 | } 39 | export default App; 40 | 41 | -------------------------------------------------------------------------------- /basicReactElementExample.js: -------------------------------------------------------------------------------- 1 | // Basic example of creating element in .js file 2 | // React.createElement has 3 part 3 | // React.createElement( /* type */, /* props */, /* nested content */ ); 4 | 5 | import React from 'react'; 6 | import ReactDOM from 'react-dom'; 7 | 8 | const element = React.createElement('div', null, 'Hello World') 9 | 10 | console.log(element) 11 | 12 | ReactDOM.render( 13 | element, 14 | document.getElementById('root') 15 | ) 16 | 17 | //**************************************************************************** 18 | // run one at a time 19 | 20 | /* 21 | import React from 'react'; 22 | import ReactDOM from 'react-dom'; 23 | 24 | const element = React.createElement('div', { 25 | className: anyCssClass 26 | }, 'Hello World') 27 | 28 | console.log(element) 29 | 30 | ReactDOM.render( 31 | element, 32 | document.getElementById('root') 33 | ) 34 | 35 | */ 36 | 37 | //**************************************************************************** 38 | // run one at a time 39 | 40 | /* 41 | 42 | const element = React.createElement("ol",{className: heading}, 43 | React.createElement("li",null, "Aamir") 44 | React.createElement("li",null, "Irfna") 45 | React.createElement("li",null, "Pinger") 46 | ); 47 | 48 | */ 49 | 50 | 51 | //*************************************************************************************** 52 | 53 | -------------------------------------------------------------------------------- /componentDidMountExample.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react' 2 | import styled from 'styled-components' 3 | 4 | import './App.css' 5 | 6 | const Button = styled.button` 7 | background-color: white; 8 | color: navy; 9 | width: 80px; 10 | height: 40px; 11 | ` 12 | 13 | const H1 = styled.h1` 14 | font-size: 36px; 15 | color: Olive; 16 | ` 17 | const Button2 = styled(Button)` 18 | background-color: silver; 19 | ` 20 | 21 | function ControlledInputBox (props) { 22 | const { handleInputChange, textBoxValue, errorShowFlag } = props 23 | 24 | return ( 25 | <> 26 | 27 | {errorShowFlag && } 28 | 29 | ) 30 | } 31 | 32 | function ButtonComponent () { 33 | return ( 34 | <> 35 | 36 | 37 | ) 38 | } 39 | 40 | function ErrorMessage (props) { 41 | const { errorMsg } = props 42 | 43 | return {errorMsg} 44 | } 45 | 46 | class MyComponent extends React.Component { 47 | state = { 48 | cityArray: [], 49 | textBoxValue: '', 50 | errorShowFlag: false 51 | } 52 | 53 | componentDidMount () { 54 | /** External API call can be done here and update the state when data received */ 55 | const dataReceivedFromAPICall = ['Karachi', 'Lahore', 'Peshawar', 'Quetta'] 56 | 57 | setTimeout(() => { 58 | this.setState({ 59 | cityArray: dataReceivedFromAPICall 60 | }) 61 | }, 1000) 62 | } 63 | 64 | handleInputChange = e => { 65 | const { value } = e.target 66 | this.setState({ 67 | textBoxValue: value, 68 | errorShowFlag: false 69 | }) 70 | } 71 | 72 | addCity = () => { 73 | const { textBoxValue } = this.state 74 | 75 | this.setState(prevState => ({ 76 | cityArray: [...prevState.cityArray, textBoxValue], 77 | textBoxValue: '' 78 | })) 79 | } 80 | 81 | removeCity = event => { 82 | const { cityArray } = this.state 83 | const { value } = event.target 84 | 85 | const newCityArray = cityArray.filter(city => city !== value) 86 | 87 | this.setState({ 88 | cityArray: newCityArray 89 | }) 90 | } 91 | 92 | handleSubmit = event => { 93 | event.preventDefault() 94 | const { textBoxValue } = this.state 95 | 96 | if (textBoxValue === '') { 97 | this.setState({ 98 | errorShowFlag: true 99 | }) 100 | } else { 101 | this.addCity() 102 | } 103 | } 104 | 105 | render () { 106 | const { cityArray, textBoxValue, errorShowFlag } = this.state 107 | const showSite = cityArray.length !== 0 108 | 109 | return !showSite ? ( 110 | 111 | ) : ( 112 | <> 113 |
    114 | {cityArray.map(city => ( 115 |
  • 120 | {city} 121 | 122 | X 123 | 124 |
  • 125 | ))} 126 |
127 |
128 | 133 | 134 | 135 | 136 | ) 137 | } 138 | } 139 | 140 | function MyFunctionalComponent (props) { 141 | const { heading } = props 142 | return

{heading}

143 | } 144 | 145 | function App () { 146 | const showSite = true 147 | 148 | if (showSite) { 149 | return ( 150 |
151 | 152 | 153 |
154 | ) 155 | } else { 156 | return 157 | } 158 | } 159 | 160 | export default App 161 | -------------------------------------------------------------------------------- /componentDidMountExample2.js: -------------------------------------------------------------------------------- 1 | import React, { Component, Fragment } from 'react' 2 | 3 | class App extends Component { 4 | state = { 5 | number: 0, 6 | } 7 | 8 | componentDidMount() { 9 | setTimeout(() => { 10 | this.setState({ 11 | number: 1000, 12 | }) 13 | }, 3000) 14 | } 15 | 16 | render() { 17 | 18 | return ( 19 | 20 |

this number is going to change in three second: 21 | 22 | {this.state.number} 23 | 24 |

25 |
26 | 27 | ) 28 | } 29 | } 30 | 31 | export default App 32 | -------------------------------------------------------------------------------- /componentDidUpdateExample.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react' 2 | import styled from 'styled-components' 3 | 4 | import './App.css' 5 | 6 | const Button = styled.button` 7 | background-color: white; 8 | color: navy; 9 | width: 80px; 10 | height: 40px; 11 | ` 12 | 13 | const H1 = styled.h1` 14 | font-size: 36px; 15 | color: Olive; 16 | ` 17 | const Button2 = styled(Button)` 18 | background-color: silver; 19 | ` 20 | 21 | function ControlledInputBox (props) { 22 | const { handleInputChange, textBoxValue, errorShowFlag } = props 23 | 24 | return ( 25 | <> 26 | 27 | {errorShowFlag && } 28 | 29 | ) 30 | } 31 | 32 | function ButtonComponent () { 33 | return ( 34 | <> 35 | 36 | 37 | ) 38 | } 39 | 40 | function ErrorMessage (props) { 41 | const { errorMsg } = props 42 | 43 | return {errorMsg} 44 | } 45 | 46 | class MyComponent extends React.Component { 47 | state = { 48 | cityArray: [], 49 | textBoxValue: '', 50 | errorShowFlag: false 51 | } 52 | 53 | componentDidMount () { 54 | /** External API call can be done here and update the state when data received */ 55 | const dataReceivedFromAPICall = [ 56 | 'Karachi', 57 | 'Lahore', 58 | 'Peshawar', 59 | 'Quetta', 60 | 'Swat' 61 | ] 62 | 63 | setTimeout(() => { 64 | this.setState({ 65 | cityArray: dataReceivedFromAPICall 66 | }) 67 | }, 1000) 68 | } 69 | 70 | componentDidUpdate (prevProps, prevState) { 71 | const cityArrayPrev = prevState.cityArray 72 | const cityArrayNew = this.state.cityArray 73 | 74 | if (cityArrayPrev !== cityArrayNew) { 75 | console.log('State updated with new data', cityArrayNew) 76 | } 77 | } 78 | 79 | handleInputChange = e => { 80 | const { value } = e.target 81 | this.setState({ 82 | textBoxValue: value, 83 | errorShowFlag: false 84 | }) 85 | } 86 | 87 | addCity = () => { 88 | const { textBoxValue } = this.state 89 | 90 | this.setState(prevState => ({ 91 | cityArray: [...prevState.cityArray, textBoxValue], 92 | textBoxValue: '' 93 | })) 94 | } 95 | 96 | removeCity = event => { 97 | const { cityArray } = this.state 98 | const { value } = event.target 99 | 100 | const newCityArray = cityArray.filter(city => city !== value) 101 | 102 | this.setState({ 103 | cityArray: newCityArray 104 | }) 105 | } 106 | 107 | handleSubmit = event => { 108 | event.preventDefault() 109 | const { textBoxValue } = this.state 110 | 111 | if (textBoxValue === '') { 112 | this.setState({ 113 | errorShowFlag: true 114 | }) 115 | } else { 116 | this.addCity() 117 | } 118 | } 119 | 120 | render () { 121 | const { cityArray, textBoxValue, errorShowFlag } = this.state 122 | const showSite = cityArray.length !== 0 123 | 124 | return !showSite ? ( 125 | 126 | ) : ( 127 | <> 128 |
    129 | {cityArray.map(city => ( 130 |
  • 135 | {city} 136 | 137 | X 138 | 139 |
  • 140 | ))} 141 |
142 |
143 | 148 | 149 | 150 | 151 | ) 152 | } 153 | } 154 | 155 | function MyFunctionalComponent (props) { 156 | const { heading } = props 157 | return

{heading}

158 | } 159 | 160 | function App () { 161 | const showSite = true 162 | 163 | if (showSite) { 164 | return ( 165 |
166 | 167 | 168 |
169 | ) 170 | } else { 171 | return 172 | } 173 | } 174 | 175 | export default App 176 | -------------------------------------------------------------------------------- /componentWillUnmountExample.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | function ComponentHw() { 4 | 5 | return

HELLO WORLD

6 | } 7 | 8 | function ComponentSlogan() { 9 | return

PAKISTAN ZINDABAD

10 | } 11 | 12 | 13 | class App extends Component { 14 | state = { 15 | number: 0, 16 | } 17 | 18 | componentDidMount() { 19 | //after 3 second this setTimeout will remove from browser DOM 20 | // and render this new on browser DOM 21 | setTimeout(() => { 22 | ReactDOM.render(, document.getElementById('root')) 23 | }, 3000) 24 | } 25 | 26 | componentWillUnmount() { 27 | // when component will be unmounted after 3 seconds 28 | //this function will invoke autometically 29 | alert("component unmount event invoked") 30 | } 31 | 32 | 33 | render() { 34 | return ( 35 | //Always remember componentWillUnmount event will be invoke 36 | //only when there is a change at browser DOM 37 | // and in our app browser DOM we have rendered through 38 | // index.js file. so to change on browser DOM we again need to 39 | // run ReactDOM.render which in this example is componentDidMount 40 | // method will run 41 | 42 | 43 | //conditional rendering below will not invoke componentWillUnmount() method 44 | // because actually is rendered and below component is just a child of 45 | // component 46 | 47 | ) 48 | } 49 | } 50 | 51 | export default App 52 | -------------------------------------------------------------------------------- /getDerivedStateFromProps.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react' 2 | import styled from 'styled-components' 3 | 4 | import './App.css' 5 | 6 | const Button = styled.button` 7 | background-color: white; 8 | color: navy; 9 | width: 80px; 10 | height: 40px; 11 | ` 12 | 13 | const H1 = styled.h1` 14 | font-size: 36px; 15 | color: Olive; 16 | ` 17 | const Button2 = styled(Button)` 18 | background-color: silver; 19 | ` 20 | 21 | function ControlledInputBox (props) { 22 | const { handleInputChange, textBoxValue, errorShowFlag } = props 23 | 24 | return ( 25 | <> 26 | 27 | {errorShowFlag && } 28 | 29 | ) 30 | } 31 | 32 | function ButtonComponent () { 33 | return ( 34 | <> 35 | 36 | 37 | ) 38 | } 39 | 40 | function ErrorMessage (props) { 41 | const { errorMsg } = props 42 | 43 | return {errorMsg} 44 | } 45 | 46 | class MyComponent extends React.Component { 47 | state = { 48 | cityArray: [], 49 | textBoxValue: '', 50 | errorShowFlag: false 51 | } 52 | 53 | static getDerivedStateFromProps (props, state) { 54 | const { cityArray } = props 55 | 56 | return { 57 | cityArray 58 | } 59 | } 60 | 61 | handleInputChange = e => { 62 | const { value } = e.target 63 | this.setState({ 64 | textBoxValue: value, 65 | errorShowFlag: false 66 | }) 67 | } 68 | 69 | addCity = () => { 70 | const { textBoxValue } = this.state 71 | 72 | this.setState(prevState => ({ 73 | cityArray: [...prevState.cityArray, textBoxValue], 74 | textBoxValue: '' 75 | })) 76 | } 77 | 78 | removeCity = event => { 79 | const { cityArray } = this.state 80 | const { value } = event.target 81 | 82 | const newCityArray = cityArray.filter(city => city !== value) 83 | 84 | this.setState({ 85 | cityArray: newCityArray 86 | }) 87 | } 88 | 89 | handleSubmit = event => { 90 | event.preventDefault() 91 | const { textBoxValue } = this.state 92 | 93 | if (textBoxValue === '') { 94 | this.setState({ 95 | errorShowFlag: true 96 | }) 97 | } else { 98 | this.addCity() 99 | } 100 | } 101 | 102 | render () { 103 | const { cityArray, textBoxValue, errorShowFlag } = this.state 104 | 105 | return ( 106 | <> 107 |
    108 | {cityArray.map(city => ( 109 |
  • 114 | {city} 115 | 116 | X 117 | 118 |
  • 119 | ))} 120 |
121 |
122 | 127 | 128 | 129 | 130 | ) 131 | } 132 | } 133 | 134 | function MyFunctionalComponent (props) { 135 | const { heading } = props 136 | return

{heading}

137 | } 138 | 139 | function App () { 140 | const showSite = true 141 | const cityArray = ['Karachi', 'Lahore', 'Peshawar', 'Quetta'] 142 | 143 | if (showSite) { 144 | return ( 145 |
146 | 147 | 148 |
149 | ) 150 | } else { 151 | return 152 | } 153 | } 154 | 155 | export default App 156 | -------------------------------------------------------------------------------- /myFirstComponent.js: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | 1st step: rename this file with the name App.js in the src folder after creating 4 | initial app with npx create-react-app 5 | 6 | 2nd step: Run npm start to run the application 7 | 8 | 3rd step: Goto to browser and type localhost:3000 9 | 10 | */ 11 | 12 | import React from 'react' 13 | import logo from './logo.svg' 14 | import './App.css' 15 | 16 | class MyComponent extends React.Component { 17 | render () { 18 | return ( 19 |
20 |

Hello world

21 |
22 | ) 23 | } 24 | } 25 | 26 | function App () { 27 | return ( 28 | 29 | ) 30 | } 31 | 32 | export default App 33 | -------------------------------------------------------------------------------- /setStateExampleCode.js: -------------------------------------------------------------------------------- 1 | //App.js 2 | 3 | 4 | // **************************************************************** 5 | // 6 | // Run following one at a time 7 | // 8 | // **************************************************************** 9 | 10 | // **************************************************************** 11 | // 12 | // setState Object base update approach 13 | // 14 | // **************************************************************** 15 | 16 | import React, { Component, Fragment } from 'react'; 17 | 18 | class App extends Component { 19 | state = { 20 | cityArray1: ["Karachi", "Lahore", "Peshawer", "Quetta"], 21 | cityArray2: ["Hyderabad", "Multan", "Gawader", "Mardan"], 22 | } 23 | 24 | render() { 25 | setTimeout(() => { 26 | // setState Object base update approach 27 | this.setState( 28 | { 29 | cityArray1: ["Pakistan", "USA", "Russia", "UK"] 30 | } 31 | ) 32 | }, 3000) 33 | 34 | return ( 35 | //You can also use
instead of 36 | 37 | 38 | 39 | 40 | ) 41 | } 42 | } 43 | 44 | class City extends Component { 45 | 46 | render() { 47 | const cityArray = this.props.cityArray 48 | // you can also do like this 49 | //const { cityArray } = this.props 50 | 51 | return ( 52 | //You can also use
instead of 53 | 54 |
    55 | { 56 | cityArray.map((city) =>
  • {city}
  • ) 57 | } 58 |
59 |

{this.props.name}

60 |
61 | ) 62 | } 63 | } 64 | export default App; 65 | 66 | 67 | 68 | // **************************************************************** 69 | // 70 | // setState functional update approach 71 | // 72 | // **************************************************************** 73 | 74 | // import React, { Component, Fragment } from 'react'; 75 | 76 | // class App extends Component { 77 | // state = { 78 | // cityArray1: ["Karachi", "Lahore", "Peshawer", "Quetta"], 79 | // cityArray2: ["Hyderabad", "Multan", "Gawader", "Mardan"], 80 | // number: 0, 81 | // } 82 | 83 | // render() { 84 | 85 | // setTimeout(() => { 86 | // // setState Functional update approach 87 | // this.setState((prevState) => ( 88 | // { 89 | // cityArray1: ["Pakistan", "USA", "Russia", "UK"], 90 | // number: prevState.number + 1 91 | // }) 92 | // ) 93 | // }, 3000) 94 | 95 | // return ( 96 | // //You can also use
instead of 97 | // 98 | // 103 | // 108 | // 109 | // ) 110 | // } 111 | // } 112 | 113 | // class City extends Component { 114 | 115 | // render() { 116 | // const { cityArray, number, name } = this.props 117 | // // you can also do like this, but above is best practice 118 | // //const cityArray = this.props.cityArray 119 | // //const number = this.props.number 120 | 121 | // return ( 122 | // //You can also use
instead of 123 | // 124 | //
    125 | // { 126 | // cityArray.map((city) =>
  • {city}
  • ) 127 | // } 128 | //
129 | //

{name}

130 | //

{number}

131 | //
132 | // ) 133 | // } 134 | // } 135 | // export default App; 136 | -------------------------------------------------------------------------------- /shouldComponentUpdateExample.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react' 2 | import styled from 'styled-components' 3 | 4 | import './App.css' 5 | 6 | const Button = styled.button` 7 | background-color: white; 8 | color: navy; 9 | width: 80px; 10 | height: 40px; 11 | ` 12 | 13 | const H1 = styled.h1` 14 | font-size: 36px; 15 | color: Olive; 16 | ` 17 | const Button2 = styled(Button)` 18 | background-color: silver; 19 | ` 20 | 21 | function ControlledInputBox (props) { 22 | const { handleInputChange, textBoxValue, errorShowFlag } = props 23 | 24 | return ( 25 | <> 26 | 27 | {errorShowFlag && } 28 | 29 | ) 30 | } 31 | 32 | function ButtonComponent () { 33 | return ( 34 | <> 35 | 36 | 37 | ) 38 | } 39 | 40 | function ErrorMessage (props) { 41 | const { errorMsg } = props 42 | 43 | return {errorMsg} 44 | } 45 | 46 | class MyComponent extends React.Component { 47 | state = { 48 | cityArray: [], 49 | textBoxValue: '', 50 | errorShowFlag: false 51 | } 52 | 53 | componentDidMount () { 54 | /** External API call can be done here and update the state when data received */ 55 | const dataReceivedFromAPICall = [ 56 | 'Karachi', 57 | 'Lahore', 58 | 'Peshawar', 59 | 'Quetta', 60 | 'Swat' 61 | ] 62 | 63 | setTimeout(() => { 64 | this.setState({ 65 | cityArray: dataReceivedFromAPICall 66 | }) 67 | }, 1000) 68 | } 69 | 70 | shouldComponentUpdate (nextProps, nextState) { 71 | const { cityArray } = nextState 72 | const findCity = cityArray.indexOf('Swat') 73 | 74 | // Remove 'Swat' from componentDidMount method and see the change on browser 75 | if (findCity === -1) return false 76 | 77 | return true 78 | } 79 | 80 | handleInputChange = e => { 81 | const { value } = e.target 82 | this.setState({ 83 | textBoxValue: value, 84 | errorShowFlag: false 85 | }) 86 | } 87 | 88 | addCity = () => { 89 | const { textBoxValue } = this.state 90 | 91 | this.setState(prevState => ({ 92 | cityArray: [...prevState.cityArray, textBoxValue], 93 | textBoxValue: '' 94 | })) 95 | } 96 | 97 | removeCity = event => { 98 | const { cityArray } = this.state 99 | const { value } = event.target 100 | 101 | const newCityArray = cityArray.filter(city => city !== value) 102 | 103 | this.setState({ 104 | cityArray: newCityArray 105 | }) 106 | } 107 | 108 | handleSubmit = event => { 109 | event.preventDefault() 110 | const { textBoxValue } = this.state 111 | 112 | if (textBoxValue === '') { 113 | this.setState({ 114 | errorShowFlag: true 115 | }) 116 | } else { 117 | this.addCity() 118 | } 119 | } 120 | 121 | render () { 122 | const { cityArray, textBoxValue, errorShowFlag } = this.state 123 | const showSite = cityArray.length !== 0 124 | 125 | return !showSite ? ( 126 | 127 | ) : ( 128 | <> 129 |
    130 | {cityArray.map(city => ( 131 |
  • 136 | {city} 137 | 138 | X 139 | 140 |
  • 141 | ))} 142 |
143 |
144 | 149 | 150 | 151 | 152 | ) 153 | } 154 | } 155 | 156 | function MyFunctionalComponent (props) { 157 | const { heading } = props 158 | return

{heading}

159 | } 160 | 161 | function App () { 162 | const showSite = true 163 | 164 | if (showSite) { 165 | return ( 166 |
167 | 168 | 169 |
170 | ) 171 | } else { 172 | return 173 | } 174 | } 175 | 176 | export default App 177 | -------------------------------------------------------------------------------- /stateWithChangeHandlerExample/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component, Fragment } from 'react'; 2 | import UserInput from './UserInput'; 3 | import Output from './Output'; 4 | 5 | 6 | class App extends Component { 7 | state = { 8 | userText: "Aamir", 9 | var2: "Pinger", 10 | } 11 | 12 | 13 | handleChange = (val) => { 14 | this.setState((prevState) => ({ 15 | ...prevState, 16 | userText: prevState.userText + ' - ' + val, 17 | })) 18 | 19 | } 20 | 21 | render() { 22 | 23 | return ( 24 | 25 | 26 |
27 |
28 | 29 |
30 | 31 | ); 32 | } 33 | } 34 | 35 | export default App; 36 | -------------------------------------------------------------------------------- /stateWithChangeHandlerExample/Output.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import typeOfProps from 'prop-types' 3 | 4 | // ************************************************************************** 5 | // 6 | // To use 'prop-types' you have to install prop-types 7 | // 8 | // npm install prop-types --save 9 | // 10 | // ************************************************************************** 11 | 12 | 13 | 14 | // class Output extends Component { 15 | const Output = (props) => { 16 | // render() { 17 | //const name = this.props.name 18 | const { name, var2 } = props 19 | 20 | return ( 21 |

{`Output : ${name}, ${var2}`}

22 | 23 | ); 24 | } 25 | // } 26 | 27 | 28 | Output.propTypes = { 29 | name: typeOfProps.oneOfType([typeOfProps.string, typeOfProps.bool]).isRequired, 30 | var2: typeOfProps.string, 31 | 32 | } 33 | 34 | export default Output; 35 | -------------------------------------------------------------------------------- /stateWithChangeHandlerExample/UserInput.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | 4 | class UserInput extends React.Component { 5 | //function UserInput(props) { 6 | 7 | state = { 8 | txt: '' 9 | } 10 | 11 | handleSubmit = () => { 12 | const { changeValue } = this.props 13 | const val = document.getElementById("userinp").value 14 | changeValue(val) 15 | } 16 | 17 | handleChange = (e) => { 18 | //***************************************************************************** 19 | // 20 | // these below commented lines are for the user input validation 21 | // these below lines will not let user input any character other then numbers 22 | // 23 | //***************************************************************************** 24 | // 25 | // if (isNaN(e.target.value)) { 26 | // console.log("only numbers") 27 | // } else { 28 | // this.setState({ txt: e.target.value }) 29 | // } 30 | 31 | //***************************************************************************** 32 | // 33 | // if you uncomment following line then first comment below line 34 | // 35 | //***************************************************************************** 36 | 37 | this.setState({ txt: e.target.value }) 38 | } 39 | render() { 40 | // const { changeValue, children } = this.props 41 | 42 | return ( 43 |
44 | 51 | 52 | 55 | 56 |
57 | ); 58 | } 59 | } 60 | 61 | export default UserInput; 62 | --------------------------------------------------------------------------------