-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotes.txt
210 lines (176 loc) · 4.79 KB
/
Notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
Class Component and State | Part 2 | Cheat Sheet
Concepts in Focus
•
setState()
Object Syntax
• Sending Function as Callback
• Input Element
• Searchable Users List Application
1. setState() Object Syntax
The
setState()
object syntax can be used while updating the state to the value that is independent of the previous state.
Syntax:
JS CODE:
this.setState(
{propertyName1: propertyValue1},
{propertyName2: propertyValue2}
// and many more...
);
1.1 Callback vs Object
Callback:
JS CODE:
this.setState(prevState => {
return { count: prevState.count + 1 };
});
It is used while updating the state to a value, which is computed based on the previous state.
Object:
JS CODE:
this.setState({ quantity: 2 });
It is used while updating the state to a static value.
2. Sending Function as Callback
We can pass functions as props to child components.
Syntax:
JSX CODE:
<ComponentName functionName={this.functionName} />
3. Input Element
In React, the Input Element value can be handled in two ways:
• Controlled Input
• Uncontrolled Input
3.1 Controlled Input
If the Input Element value is handled by a React State then it is called Controlled Input. Controlled Inputs are the React Suggested way to handle Input Element value.
Example:
JSX CODE:
import {Component} from 'react'
class App extends Component {
state = {
searchInput: '',
}
onChangeSearchInput = event => {
this.setState({
searchInput: event.target.value,
})
}
render() {
const {searchInput} = this.state
return (
<input
type="text"
onChange={this.onChangeSearchInput}
value={searchInput}
/>
)
}
}
export default App
3.2 Uncontrolled Input
If the Input Element value is handled by the browser itself then it is called Uncontrolled Input.
Uncontrolled inputs are like traditional HTML form inputs. Its valuecan only be set by a user, but not programmatically. However, in controlled input value is programmatically handled using React State.
Example:
JSX CODE:
<input type="text" />
4. Searchable Users List Application
File: src/App.js
JSX CODE:
import {Component} from 'react'
import UserProfile from './components/UserProfile'
import './App.css'
const initialUserDetailsList = [
{
uniqueNo: 1,
imageUrl: 'https://assets.ccbp.in/frontend/react-js/esther-howard-img.png',
name: 'Esther Howard',
role: 'Software Developer'
},
{
uniqueNo: 2,
imageUrl: 'https://assets.ccbp.in/frontend/react-js/floyd-miles-img.png',
name: 'Floyd Miles',
role: 'Software Developer'
},
{
uniqueNo: 3,
imageUrl: 'https://assets.ccbp.in/frontend/react-js/jacob-jones-img.png',
name: 'Jacob Jones',
role: 'Software Developer'
},
{
uniqueNo: 4,
imageUrl: 'https://assets.ccbp.in/frontend/react-js/devon-lane-img.png',
name: 'Devon Lane',
role: 'Software Developer'
}
]
class App extends Component {
state = {
searchInput: '',
usersDetailsList: initialUserDetailsList
}
onChangeSearchInput = event => {
this.setState({
searchInput: event.target.value
})
}
deleteUser = uniqueNo => {
const {usersDetailsList} = this.state
const filteredUsersData = usersDetailsList.filter(
each => each.uniqueNo !== uniqueNo
)
this.setState({
usersDetailsList: filteredUsersData
})
}
render() {
const {searchInput, usersDetailsList} = this.state
const searchResults = usersDetailsList.filter(eachUser =>
eachUser.name.includes(searchInput)
)
return (
<div className="app-container">
<h1 className="title">Users List</h1>
<input
type="search"
onChange={this.onChangeSearchInput}
value={searchInput}
/>
<ul className="list-container">
{searchResults.map(eachUser => (
<UserProfile
userDetails={eachUser}
key={eachUser.uniqueNo}
deleteUser={this.deleteUser}
/>
))}
</ul>
</div>
)
}
}
export default App
File: src/components/UserProfile/index.js
JSX CODE:
import './index.css'
const UserProfile = props => {
const {userDetails, deleteUser} = props
const {imageUrl, name, role, uniqueNo} = userDetails
const onDelete = () => {
deleteUser(uniqueNo)
}
return (
<li className="user-card-container">
<img src={imageUrl} className="profile-pic" alt="profile-pic" />
<div className="user-details-container">
<h1 className="user-name"> {name} </h1>
<p className="user-designation"> {role} </p>
</div>
<button className="delete-button" onClick={onDelete}>
<img
src="https://assets.ccbp.in/frontend/react-js/cross-img.png"
alt="cross"
className="delete-img"
/>
</button>
</li>
)
}
export default UserProfile