Repository navigation
Expand file tree
/
Copy pathstate
More file actions
227 lines (169 loc) · 6.15 KB
/
Copy pathstate
File metadata and controls
227 lines (169 loc) · 6.15 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
1)
import React from "react";
import Header from "./Components/Header";
import Meme from "./Components/Meme.js";
export default function App(){
const [count ,setCount ]=React.useState(0)
function add(){
setCount(function(oldValue){
return oldValue + 1
})
}
function sub(){
setCount(oldvalue => oldvalue -1)
}
return <div>
<div>
<h1>Add or Subtract </h1>
<h1 onClick={add}>+</h1>
<button>{count}</button>
<h1 onClick={sub}>-</h1>
<h2></h2>
</div>
</div>
}
///// /// another user of state different method ////////////
2)
import React from "react";
export default function App(){
const [IsExited , ChangeFunction ]= React.useState(true)
return <div>
<div>are you feelling exited to learn reat </div>
<div></div>
<div onClick={() => { ChangeFunction(prestate => !prestate)}}>{IsExited === true ? "yes" :"no"}</div>
</div>
}
// the above expression is done in short hand properties of the followind code
// function onclickHere(){
// changeFunction(prestate =>!prestate)
// }
//////using state inside state (state bhitra state) to add the items in react /////////
3)
import React from "react";
export default function Test(){
// using state
const [ThingsArray, setThingsElement] = React.useState(["things 1" ,"things 2", "things 3"])
const thingsElemtnt = ThingsArray.map( Things => <p key={Things}>{Things}</p>)
function addItems(){
setThingsElement(prestate => {
return[...prestate , `things ${ThingsArray.length + 1}`]
})
// Here meaning of ...prestate is spreading the pre existing array
}
return <div>
<button onClick={addItems}>Click</button>
{thingsElemtnt}
</div>
}
4)
////// building a meme generator Complex state :Object /////// state with object
import React from 'react'
export default function Apps(){
const [contact ,setContact] = React.useState({
firstName : "Padam",
lastName : "Damai",
phone : "984089821",
email : "padamdamai888@gmail.com",
isFavourite : false
})
let starIcon = contact.isFavourite? "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e5/Full_Star_Yellow.svg/800px-Full_Star_Yellow.svg.png":"https://clipart-library.com/images/LcdoE7Ebi.gif"
return <main>
<article>
<img src= "https://clipart-library.com/images/kTKo7BB8c.png" className='article-image'/>
<div className='card-info'>
<img className='star-img' src={starIcon}/>
<h1 className='card-name'>{contact.firstName} {contact.lastName}</h1>
<div className='card-small-contact'>
<p className="card-contact">{contact.phone}</p>
<div className='card-gmail'>{contact.email}</div>
</div>
</div>
</article>
</main>
}
5)
// updating state object
import React from 'react'
export default function Apps(){
const [contact ,setContact] = React.useState({
firstName : "Padam",
lastName : "Damai",
phone : "984089821",
email : "padamdamai888@gmail.com",
isFavourite : false
})
function ToggleIcon(){
setContact(preContact => {
return {
// firstName : "Padam",
// lastName : "Damai",
// phone : "984089821",
// email : "padamdamai888@gmail.com",
// isFavourite : false
// isFavourite : !preContact.isFavourite
...preContact, // expandng the contact information or including the content (if we didnt include this code than there wont be any content displayed)
isFavourite : !preContact.isFavourite
}
})
// this is obtainer as similar as the above given
}
let starIcon = contact.isFavourite? "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e5/Full_Star_Yellow.svg/800px-Full_Star_Yellow.svg.png":"https://clipart-library.com/images/LcdoE7Ebi.gif"
return <main>
<article>
<img src= "https://clipart-library.com/images/kTKo7BB8c.png" className='article-image'/>
<div className='card-info'>
<img className='star-img' onClick={ToggleIcon} src={starIcon}/>
<h1 className='card-name'>{contact.firstName} {contact.lastName}</h1>
<div className='card-small-contact'>
<p className="card-contact">{contact.phone}</p>
<div className='card-gmail'>{contact.email}</div>
</div>
</div>
</article>
</main>
}
////////////////////// using props and state in above program i.e(5)
/////////new file StarSelect.js file created
import React from "react";
export default function Star(props){
const starIcon = props.isFilled? "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e5/Full_Star_Yellow.svg/800px-Full_Star_Yellow.svg.png":"https://clipart-library.com/images/LcdoE7Ebi.gif"
return <div>
<img className='star-img' onClick={props.handleClick} src={starIcon}/>
</div>
}
///////////main files app.js
import React from 'react'
import Star from './Components/StarSelect.js'
export default function Apps(){
const [contact ,setContact] = React.useState({
firstName : "Padam",
lastName : "Damai",
phone : "984089821",
email : "padamdamai888@gmail.com",
isFavourite : false
})
function ToggleIcon(){
setContact(preContact => {
return {
...preContact, // expandng the contact information or including the content (if we didnt include this code than there wont be any content displayed)
isFavourite : !preContact.isFavourite
}
})
// this is obtainer as similar as the above given
}
// let starIcon = contact.isFavourite? "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e5/Full_Star_Yellow.svg/800px-Full_Star_Yellow.svg.png":"https://clipart-library.com/images/LcdoE7Ebi.gif"
return <main>
<article>
<img src= "https://clipart-library.com/images/kTKo7BB8c.png" className='article-image'/>
<div className='card-info'>
{/* <img className='star-img' onClick={ToggleIcon} src={starIcon}/> */}
<Star isFilled={contact.isFavourite} handleClick= {ToggleIcon}/>
<h1 className='card-name'>{contact.firstName} {contact.lastName}</h1>
<div className='card-small-contact'>
<p className="card-contact">{contact.phone}</p>
<div className='card-gmail'>{contact.email}</div>
</div>
</div>
</article>
</main>
}