Skip to content

Commit dc4c3e2

Browse files
Added some stuff to lesson_2 and lesson_1
1 parent e1b1e25 commit dc4c3e2

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

lesson_1/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,20 @@ class ExampleComponent extends Component {
4040
return <h1>Hey, this is an example component</h1>;
4141
}
4242
}
43+
44+
export default ExampleComponent;
4345
```
4446

4547
Noticed anything different about the above snippet of code?
4648

4749
The above code is written in JSX Syntax. While you can write React without JSX syntax, JSX makes it elagant.
4850

51+
You could also use React with plain javascript as follows:
52+
```javascript
53+
React.createElement('a', {href: 'mailto:xyz@example.com'}, 'xyz@example.com');
54+
```
55+
56+
Its better to use JSX syntax to keep the code clean and readable.
4957

5058
Understanding Component Life Cycle:
5159

lesson_2/README.md

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ React components need data to work with. There are two ways of combining compone
1515
Example:
1616

1717
```javascript
18+
import React from 'react';
19+
1820
const Image = (props) => { // Arrow functions*
1921

2022
return <img src={props.url}></img>;
2123
}
2224

2325
// Usage:
2426
const xyz = () => {
25-
return <Image url="https://static.boredpanda.com/blog/wp-content/uploads/2014/04/irony-funny-pictures-40.jpg" />
27+
return <Image url="https://static.boredpanda.com/blog/wp-content/uploads/2014/04/irony-funny-pictures-40.jpg"
28+
/>
2629
}
2730
```
2831

@@ -33,4 +36,68 @@ React components need data to work with. There are two ways of combining compone
3336

3437
State is an object owned by the component and its scope is limited to the component. A component can initialize the state and update it when required.
3538

36-
![alt text](res/state.jpg)
39+
![alt text](res/state.jpg)
40+
41+
Example:
42+
```javascript
43+
import React, {Component} from 'react';
44+
45+
class StatefulExample extends Component {
46+
47+
constructor(props){
48+
super(props);
49+
this.state = {
50+
count: 0,
51+
};
52+
}
53+
54+
onClick = () => {
55+
this.setState({
56+
count: this.state.count + 1, // this updates count
57+
});
58+
}
59+
60+
render(){
61+
const { title } = this.props;
62+
const { count } = this.state;
63+
64+
return (<div>
65+
<h2>{title}</h2>
66+
<button>{`I was clicked ${count} times`}</button>{/* Template literal */}
67+
</div>);
68+
}
69+
70+
}
71+
72+
```
73+
74+
[Template Literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)
75+
76+
### When to use what?
77+
78+
It all comes down to presentational and container components.
79+
80+
**Presentational Components** don't specify how data should be loaded or mutated.They simply receive data via props.They do not have dependencies. They are written as functional components(statesless). They may contain presentational or container components inside.
81+
82+
Example:
83+
```javascript
84+
85+
import React from 'react';
86+
87+
const List = (props) => {
88+
return (<ul>
89+
{
90+
props.items.map(
91+
(item, index)=>{
92+
return (<li>
93+
{item}
94+
</li>);
95+
})
96+
}
97+
</ul>);
98+
}
99+
100+
101+
```
102+
103+
**Container Components**

0 commit comments

Comments
 (0)