momin-riyadh 5 years ago
commit 8077b9646a

1
.gitignore vendored

@ -1,6 +1,5 @@
draft.md
react-for-everyone.md
component.md
11_Day_Events
12_Day_Forms

@ -1,4 +1,4 @@
# 30 Days of React App: Day 3
# 30 Days of React App: Day 4
In the project directory, you can run to start the project

@ -1,4 +1,4 @@
# 30 Days of React App: Day 3
# 30 Days of React App: Day 5
In the project directory, you can run to start the project

@ -1,4 +1,4 @@
# 30 Days of React App: Day 3
# 30 Days of React App: Day 6
In the project directory, you can run to start the project

@ -1,4 +1,4 @@
# 30 Days of React App: Day 3
# 30 Days of React App: Day 7
In the project directory, you can run to start the project

@ -1,4 +1,4 @@
# 30 Days of React App: Day 3
# 30 Days of React App: Day 8
In the project directory, you can run to start the project

@ -119,11 +119,10 @@ const buttonStyles = {
padding: 10,
border: 'none',
borderRadius: 5,
margin: 3,
margin: '3px auto',
cursor: 'pointer',
fontSize: 22,
color: 'white',
margin: '0 auto',
}
// class based component
@ -225,11 +224,10 @@ const buttonStyles = {
padding: 10,
border: 'none',
borderRadius: 5,
margin: 3,
margin: '3px auto',
cursor: 'pointer',
fontSize: 22,
color: 'white',
margin: '0 auto',
}
// class based component
@ -325,11 +323,10 @@ const buttonStyles = {
padding: 10,
border: 'none',
borderRadius: 5,
margin: 3,
margin: '3px auto',
cursor: 'pointer',
fontSize: 22,
color: 'white',
margin: '0 auto',
}
// class based component
@ -434,11 +431,10 @@ const buttonStyles = {
padding: 10,
border: 'none',
borderRadius: 5,
margin: 3,
margin: '3px auto',
cursor: 'pointer',
fontSize: 22,
color: 'white',
margin: '0 auto',
}
// class based component
@ -663,11 +659,10 @@ const buttonStyles = {
padding: 10,
border: 'none',
borderRadius: 5,
margin: 3,
margin: '3px auto',
cursor: 'pointer',
fontSize: 22,
color: 'white',
margin: '0 auto',
}
// Footer Component

@ -1,4 +1,4 @@
# 30 Days of React App: Day 3
# 30 Days of React App: Day 9
In the project directory, you can run to start the project

@ -127,11 +127,10 @@ const buttonStyles = {
padding: 10,
border: 'none',
borderRadius: 5,
margin: 3,
margin: '3px auto',
cursor: 'pointer',
fontSize: 22,
color: 'white',
margin: '0 auto',
}
// Footer Component

@ -18,9 +18,18 @@
![30 Days of React banner](../images/30_days_of_react_banner_day_10.jpg)
- [React Project Folder Structure and File Naming](#react-project-folder-structure-and-file-naming)
- [File Naming](#file-naming)
- [Folder](#folder)
- [Components Folder](#components-folder)
- [Fragments](#fragments)
- [Exercises](#exercises)
- [Exercises:Level 1](#exerciseslevel-1)
- [Exercises:Level 2](#exerciseslevel-2)
# React Project Folder Structure and File Naming
There is no strictly to use a single folder structure or file naming in React project. Most of the time, these kind of choice can be made by a team or a company may have a guideline. There is no right or wrong way of structuring a React project but some structures are better than the others for scalability, ease of working on files and easy to understand structure. If you like to learn further about folder structure you may check the following articles.
There is no strict way to use a single folder structure or file naming in React project. Most of the time, these kind of choice can be made by a team. Sometimes a company may have a developed guidelines about what code conventions to follow, folder structure and file naming. There is no right or wrong way of structuring a React project but some structures are better than the others for scalability,maintainability, ease of working on files and easy to understand structure. If you like to learn further about folder structure you may check the following articles.
- [React Folder Structure by https://www.devaradise.com ](https://www.devaradise.com/react-project-folder-structure)
- [React Folder Structure by www.robinwieruch.de ](https://www.robinwieruch.de/react-folder-structure)
@ -35,9 +44,9 @@ In all my React project, I will use CamelCase file name for all components. I pr
## Folder
I found it easy to put all images, icons and fonts in an asset and all CSS style sheets in styles folder. All components will be in a component folder.
I found it easy to put all images, icons and fonts in the assets folder and all CSS style sheets in styles folder. All components will be in the components folder.
So far, we have been working on index.js file. We have lots of component on index.js. Today we will move every component to a single file. We import all the file to App.js. In the process, you will see my folder structure. We are in src directory. All the folder structure will be inside the src directory. Let's start from the index.js file. In addition to index.js file, let's create an App.js file and move most of the components we had to App.js for the time being.
So far, we have been working on index.js file. We have lots of component on index.js. Today we will move every component to a single file and we will import all the files to App.js. In the process, you will see my folder structure. Currently, we are at src directory. All the folder structure will be inside the src directory. Let's start from the index.js file. In addition to index.js file, let's create an App.js file and move most of the components we had to App.js for the time being.
The index.js is your getaway to connect the component with index.html.
```js
@ -55,7 +64,7 @@ ReactDOM.render(<App />, rootElement)
In the above snippet of code, we have the App component. Let's create the App component to its own file, App.js
```js
//src/App.js
// src/App.js
import React from 'react
const App = () => <h1>Welcome to 30 Days Of React</h1>
```
@ -65,10 +74,22 @@ We have to export the component to import it in another file. We can export it a
We just add the keyword export before _let_ or _const_ to make a named export.
```js
//src/App.js
// src/App.js
import React from 'react
// named export in arrow function
export const App = () => <h1>Welcome to 30 Days Of React</h1>
```
Exporting in a function declaration, a regular function
```js
// src/App.js
import React from 'react
// named export in regular function, function declaration
export function App () {
return <h1>Welcome to 30 Days Of React</h1>
}
```
Now, let's import the App component from the App.js file to index.js.
@ -79,8 +100,6 @@ import React from 'react'
import ReactDOM from 'react-dom'
import { App } from './App'
// class based component
const rootElement = document.getElementById('root')
ReactDOM.render(<App />, rootElement)
```
@ -88,20 +107,31 @@ ReactDOM.render(<App />, rootElement)
We saw a named export and now let's implement it with default export. We can do it in two ways but it is recommended to use the second way if we are exporting components because sometimes we may bind a component with another higher order component.
```js
//src/App.js
// src/App.js
import React from 'react
// export default in arrow function
export default const App = () => <h1>Welcome to 30 Days Of React</h1>
```
```js
//src/App.js
// src/App.js
import React from 'react
// export default in arrow function
export default function App () {
return <h1>Welcome to 30 Days Of React</h1>
}
```
```js
// src/App.js
// Recommended for most of the cases
import React from 'react
const App = () => <h1>Welcome to 30 Days Of React</h1>
const App = () => <h1>Welcome to 30 Days Of React</h1>
export default App
```
If a component is exported as default we do not need curly bracket during import.
If a component is exported as default we do not need curly bracket during importing.
```js
// index.js
@ -109,8 +139,6 @@ import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
// class based component
const rootElement = document.getElementById('root')
ReactDOM.render(<App />, rootElement)
```
@ -124,7 +152,7 @@ import ReactDOM from 'react-dom'
import asabenehImage from './images'
import { countriesData } from './data/countries'
// class based component
// Header component
class Header extends React.Component {
render() {
console.log(this.props.data)
@ -197,6 +225,7 @@ const UserCard = () => (
<h2>Asabeneh Yetayeh</h2>
</div>
)
// Hexadecimal color generator
const hexaColor = () => {
let str = '0123456789abcdef'
@ -468,9 +497,110 @@ export default Header
Similar to the Header let's move all the components to their respective files.
All the CSS files on index.html will moved into styles folder and after that each part has been split its respective file, try to check the styles folder.
## Fragments
Fragments are a way to avoid unnecessary parent element in JSX. Let's implement a fragment. We import fragment from react module. As you can see below, we imported React and fragment together by use a comma separation.
```js
import React, { Fragment } from 'react'
const Skills = () => {
return (
<Fragment>
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
</Fragment>
)
}
const RequiredSkills = () => {
return (
<ul>
<Skills />
</ul>
)
}
```
It is also possible to just extract the fragment module from React as shown below.
```js
import React from 'react'
const Skills = () => {
return (
<React.Fragment>
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
</React.Fragment>
)
}
const RequiredSkills = () => {
return (
<ul>
<Skills />
</ul>
)
}
```
In latest version of Reacts it also possible to write without extracting or importing using this signs(<> </>)
```js
import React from 'react'
// Recommended
const Skills = () => {
return (
<>
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
</>
)
}
const RequiredSkills = () => {
return (
<ul>
<Skills />
</ul>
)
}
```
When we make a class-based component we have been using React.Component instead we can just import the component and the code will look more clean. Let's see an example.
```js
import React from 'react'
// without importing the Component
// Not recommended
class App extends React.Component {
render() {
return <h1> 30 Days of React </h1>
}
}
```
```js
import React, { Component } from 'react'
// This is recommended
class App extends Component {
render() {
return <h1> 30 Days of React </h1>
}
}
```
Well done. Time to do some exercises for your brain and muscles.
# Exercises
## Exercises
## Exercises:Level 1
1. What is the importance of React Folder Structure and File Naming
2. How do we export file
@ -479,10 +609,10 @@ All the CSS files on index.html will moved into styles folder and after that eac
5. Make a component or module and import it
6. Change all the components you have to different folder structure
🎉 CONGRATULATIONS ! 🎉
## Exercises:Level 2
[<< Day 9](../09_Day_Conditional_Rendering/09_conditional_rendering.md) | [Day 11 >>](../11_Day_Events/10_events.md)
1. Make a simple a simple portfolio using the components we have created so far.
```
🎉 CONGRATULATIONS ! 🎉
```
[<< Day 9](../09_Day_Conditional_Rendering/09_conditional_rendering.md) | [Day 11 >>](../11_Day_Events/10_events.md)

@ -1,4 +1,4 @@
# 30 Days of React App: Day 3
# 30 Days of React App: Day 10
In the project directory, you can run to start the project

@ -0,0 +1,230 @@
<div align="center">
<h1> 30 Days Of React: Statet</h1>
<a class="header-badge" target="_blank" href="https://www.linkedin.com/in/asabeneh/">
<img src="https://img.shields.io/badge/style--5eba00.svg?label=LinkedIn&logo=linkedin&style=social">
</a>
<a class="header-badge" target="_blank" href="https://twitter.com/Asabeneh">
<img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/asabeneh?style=social">
</a>
<sub>Author:
<a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a><br>
<small> October, 2020</small>
</sub>
</div>
[<< Day 10](../10_React_Project_Folder_Structure/10_react_project_folder_structure.md) | [Day 12 >>]()
![30 Days of React banner](../images/30_days_of_react_banner_day_8.jpg)
- [Events](#events)
- [What is an event?](#what-is-an-event)
- [Exercises](#exercises)
- [Exercises: Level 1](#exercises-level-1)
- [Exercises: Level 2](#exercises-level-2)
- [Exercises: Level 3](#exercises-level-3)
# Events
## What is an event?
An event is an action or occurrence recognized by a software. To make an event more clear let's use the daily activities we do when we use a computer such as clicking on a button, hover on an image, pressing a keyboard, scrolling the mouse wheel and etc. In this section, we will focus only some of the mouse and keyboard events. The react documentation has already a detail note about [events](https://reactjs.org/docs/handling-events.html).
Handling events in React is very similar to handling elements on DOM elements using pure JavaScript. Some of the syntax difference between handling event in React and pure JavaScript:
- React events are named using camelCase, rather than lowercase.
- With JSX you pass a function as the event handler, rather than a string.
Let's see some examples to understand event handling.
Event handling in HTML
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>30 Days Of React App</title>
</head>
<body>
<button>onclick="greetPeople()">Greet People</button>
<script>
const greetPeople = () => {
alert('Welcome to 30 Days Of React Challenge')
}
</script>
</body>
</html>
```
In React, it is slightly different
```js
import React from 'react'
// if it is functional components
const App = () => {
const greetPeople = () => {
alert('Welcome to 30 Days Of React Challenge')
}
return <button onClick={greetPeople}> </button>
}
```
```js
import React, { Component } from 'react'
// if it is functional components
class App extends Component {
greetPeople = () => {
alert('Welcome to 30 Days Of React Challenge')
}
render() {
return <button onClick={this.greetPeople}> </button>
}
}
```
Another difference between HTML and React event is that you cannot return false to prevent default behavior in React. You must call preventDefault explicitly. For example, with plain HTML, to prevent the default link behavior of opening a new page, you can write:
Plain HTML
```html
<a href="#" onclick="console.log('The link was clicked.'); return false">
Click me
</a>
```
However, in React it could be as follows:
```js
import React, { Component } from 'react'
// if it is functional components
class App extends Component {
handleClick = () => {
alert('Welcome to 30 Days Of React Challenge')
}
render() {
return (
<a href='#' onClick={this.handleClick}>
Click me
</a>
)
}
}
```
Event handling is a very vast topic and in this challenge we will focus on the most common event types. We may use the following mouse and keyboard events.
_onMouseMove, onMouseEnter, onMouseLeave, onMouseOut, onClick, onKeyDown, onKeyPress, onKeyUp, onCopy, onCut, onDrag, onChange,onBlur,onInput, onSubmit_
Let's implement some more mouse and keyboard events.
```js
// index.js
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
/*
_onMouseMove, onMouseEnter, onMouseLeave, onMouseOut, onClick, onKeyDown, onKeyPress, onKeyUp, onCopy, onCut, onDrag, onChange,onBlur,onInput, onSubmit_
*/
class App extends Component {
state = {
firstName: '',
message: '',
key: '',
}
handleClick = (e) => {
// e gives an event object
// check the value of e using console.log(e)
this.setState({
message: 'Welcome to the world of events',
})
}
// triggered whenever the mouse moves
handleMouseMove = (e) => {
this.setState({ message: 'mouse is moving' })
}
// to get value when an input field changes a value
handleChange = (e) => {
this.setState({
firstName: e.target.value,
message: e.target.value,
})
}
// to get keyboard key code when an input field is pressed
// it works with input and textarea
handleKeyPress = (e) => {
this.setState({
message:
`${e.target.value} has been pressed and the keycode is` + e.charCode,
})
}
// Blurring happens when a mouse leave an input field
handleBlur = (e) => {
this.setState({ message: 'Input field has been blurred' })
}
// This event triggers during a text copy
handleCopy = (e) => {
this.setState({
message: 'Using 30 Days Of React for commercial purpose is not allowed',
})
}
render() {
return (
<div>
<h1>Welcome to the World of Events</h1>
<button onClick={this.handleClick}>Click Me</button>
<button onMouseMove={this.handleMouseMove}>Move mouse on me</button>
<p onCopy={this.handleCopy}>
Check copy right permission by copying this text
</p>
<p>{this.state.message}</p>
<label htmlFor=''> Test for onKeyPress Event: </label>
<input type='text' onKeyPress={this.handleKeyPress} />
<br />
<label htmlFor=''> Test for onBlur Event: </label>
<input type='text' onBlur={this.handleBlur} />
<form onSubmit={this.handleSubmit}>
<div>
<label htmlFor='firstName'>First Name: </label>
<input
onChange={this.handleChange}
name='firstName'
value={this.state.value}
/>
</div>
<div>
<input type='submit' value='Submit' />
</div>
</form>
</div>
)
}
}
const rootElement = document.getElementById('root')
// we render the JSX element using the ReactDOM package
ReactDOM.render(<App />, rootElement)
```
# Exercises
## Exercises: Level 1
## Exercises: Level 2
Implement the following using onMouseEnter event
![On mouse enter event](../images/react_event_on_mouse_enter.gif)
## Exercises: Level 3
🎉 CONGRATULATIONS ! 🎉
[<< Day 10](../10_React_Project_Folder_Structure/10_react_project_folder_structure.md) | [Day 12 >>]()

@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*

@ -0,0 +1,5 @@
# 30 Days of React App: Day 3
In the project directory, you can run to start the project
### `npm start`

@ -0,0 +1,34 @@
{
"name": "30-days-of-react",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<title>30 Days Of React App</title>
</head>
<body>
<div id="root"></div>
</body>
</html>

File diff suppressed because it is too large Load Diff

@ -0,0 +1,13 @@
export const tenHighestPopulation = [
{ country: 'World', population: 7693165599 },
{ country: 'China', population: 1377422166 },
{ country: 'India', population: 1295210000 },
{ country: 'United States of America', population: 323947000 },
{ country: 'Indonesia', population: 258705000 },
{ country: 'Brazil', population: 206135893 },
{ country: 'Pakistan', population: 194125062 },
{ country: 'Nigeria', population: 186988000 },
{ country: 'Bangladesh', population: 161006790 },
{ country: 'Russian Federation', population: 146599183 },
{ country: 'Japan', population: 126960000 },
]

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

@ -0,0 +1,87 @@
// index.js
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
class App extends Component {
state = {
firstName: '',
message: '',
key: '',
}
handleClick = (e) => {
// e gives an event object
// check the value of e using console.log(e)
this.setState({
message: 'Welcome to the world of events',
})
}
// triggered whenever the mouse moves
handleMouseMove = (e) => {
this.setState({ message: 'mouse is moving' })
}
// to get value when an input field changes a value
handleChange = (e) => {
this.setState({
firstName: e.target.value,
message: e.target.value,
})
}
// to get keyboard key code when an input field is pressed
// it works with input and textarea
handleKeyPress = (e) => {
this.setState({
message:
`${e.target.value} has been pressed and the keycode is` + e.charCode,
})
}
// Blurring happens when a mouse leave an input field
handleBlur = (e) => {
this.setState({ message: 'Input field has been blurred' })
}
// This event triggers during a text copy
handleCopy = (e) => {
this.setState({
message: 'Using 30 Days Of React for commercial purpose is not allowed',
})
}
render() {
return (
<div>
<h1>Welcome to the World of Events</h1>
<button onClick={this.handleClick}>Click Me</button>
<button onMouseMove={this.handleMouseMove}>Move mouse on me</button>
<p onCopy={this.handleCopy}>
Check copy right permission by copying this text
</p>
<p>{this.state.message}</p>
<label htmlFor=''> Test for onKeyPress Event: </label>
<input type='text' onKeyPress={this.handleKeyPress} />
<br />
<label htmlFor=''> Test for onBlur Event: </label>
<input type='text' onBlur={this.handleBlur} />
<form onSubmit={this.handleSubmit}>
<div>
<label htmlFor='firstName'>First Name: </label>
<input
onChange={this.handleChange}
name='firstName'
value={this.state.value}
/>
</div>
<div>
<input type='submit' value='Submit' />
</div>
</form>
</div>
)
}
}
const rootElement = document.getElementById('root')
ReactDOM.render(<App />, rootElement)

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 113 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 113 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 113 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 113 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 113 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 723 KiB

@ -25,7 +25,7 @@
|00|[Introduction](#introduction)<br> [How to Use Repo](#how-to-use-repo)<br> [Requirements](#requirements)<br> [Setup](#setup)|
|01|[JavaScript Refresher](./01_Day_JavaScript_Refresher/01_javascript_refresher.md)|
|02|[Getting Started React](./02_Day_Introduction_to_React/02_introduction_to_react.md)|
|03|[Setting Up](./03_Day_Setting_Up/03_day_setting_up.md)|
|03|[Setting Up](./03_Day_Setting_Up/03_setting_up.md)|
|04|[Components](./04_Day_Component/04_components.md)|
|05|[Props](./05_Day_Props/05_props.md)|
|06|[List, Map and Keys](./06_Day_Map_List_Keys/06_map_list_keys.md)|
@ -33,16 +33,17 @@
|08|[States](./08_Day_States/08_states.md)|
|09|[Conditional Rendering](./09_Day_Conditional_Rendering/09_conditional_rendering.md)|
|10|[React Project Folder Structure](./10_React_Project_Folder_Structure/10_react_project_folder_structure.md)|
|11|[Events 😞]()|
|11|[Events](./11_Day_Events/11_events.md)|
|12|[Forms 😞]()|
|13|[Controlled and Uncondrolled Component 😞]()|
|13|[Component Life Cycles😞]()|
🧡🧡🧡 HAPPY CODING 🧡🧡🧡<div>
🧡🧡🧡 HAPPY CODING 🧡🧡🧡
<div>
<small>Support [**Asabeneh**](https://www.patreon.com/asabeneh?fan_landing=true) to create more educational materials</small>
[<img src = './images/become_patreon.png' alt='become-asabeneh-patreon' title='click' />](https://www.patreon.com/asabeneh?fan_landing=true)
</div>
---

Loading…
Cancel
Save