-
Notifications
You must be signed in to change notification settings - Fork 18
/
getDerivedStateFromProps.js
executable file
·155 lines (130 loc) · 3.15 KB
/
getDerivedStateFromProps.js
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
import React, { Component } from 'react'
import styled from 'styled-components'
import './App.css'
const Button = styled.button`
background-color: white;
color: navy;
width: 80px;
height: 40px;
`
const H1 = styled.h1`
font-size: 36px;
color: Olive;
`
const Button2 = styled(Button)`
background-color: silver;
`
function ControlledInputBox (props) {
const { handleInputChange, textBoxValue, errorShowFlag } = props
return (
<>
<input type='text' value={textBoxValue} onChange={handleInputChange} />
{errorShowFlag && <ErrorMessage errorMsg='Please enter city name' />}
</>
)
}
function ButtonComponent () {
return (
<>
<Button>Add City</Button>
</>
)
}
function ErrorMessage (props) {
const { errorMsg } = props
return <span style={{ color: 'red' }}>{errorMsg}</span>
}
class MyComponent extends React.Component {
state = {
cityArray: [],
textBoxValue: '',
errorShowFlag: false
}
static getDerivedStateFromProps (props, state) {
const { cityArray } = props
return {
cityArray
}
}
handleInputChange = e => {
const { value } = e.target
this.setState({
textBoxValue: value,
errorShowFlag: false
})
}
addCity = () => {
const { textBoxValue } = this.state
this.setState(prevState => ({
cityArray: [...prevState.cityArray, textBoxValue],
textBoxValue: ''
}))
}
removeCity = event => {
const { cityArray } = this.state
const { value } = event.target
const newCityArray = cityArray.filter(city => city !== value)
this.setState({
cityArray: newCityArray
})
}
handleSubmit = event => {
event.preventDefault()
const { textBoxValue } = this.state
if (textBoxValue === '') {
this.setState({
errorShowFlag: true
})
} else {
this.addCity()
}
}
render () {
const { cityArray, textBoxValue, errorShowFlag } = this.state
return (
<>
<ul style={{ border: '2px dotted navy', padding: '20px' }}>
{cityArray.map(city => (
<li
key={city}
className='listItem'
style={{ color: cityArray.length < 6 ? 'blue' : 'red' }}
>
{city}
<Button2 value={city} onClick={this.removeCity}>
X
</Button2>
</li>
))}
</ul>
<form onSubmit={this.handleSubmit}>
<ControlledInputBox
errorShowFlag={errorShowFlag}
textBoxValue={textBoxValue}
handleInputChange={this.handleInputChange}
/>
<ButtonComponent />
</form>
</>
)
}
}
function MyFunctionalComponent (props) {
const { heading } = props
return <H1 style={{ textDecoration: 'underline' }}>{heading}</H1>
}
function App () {
const showSite = true
const cityArray = ['Karachi', 'Lahore', 'Peshawar', 'Quetta']
if (showSite) {
return (
<div>
<MyFunctionalComponent heading='Cities List' />
<MyComponent cityArray={cityArray} />
</div>
)
} else {
return <ErrorMessage errorMsg='Site under maintenance' />
}
}
export default App