You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -47,4 +47,6 @@ In this repository, I've implemented (or implementing) all the concepts related
47
47
3. Affecting `state` from a generated List of Components
48
48
1. Updating State Directly (*flawed approach, can lead to unknown behaviour of the apps*): [Commit Details](https://github.com/Ch-sriram/react/commit/91c6424f4bdce8b71542c0a6cdb396475c6adff7)
49
49
2. Updating State Immutably (correction to the flawed approach) using `splice()` or `...`[Spread Operator]: [Commit Details](https://github.com/Ch-sriram/react/commit/0936c2c3b540f808a7139ec400bfe1dbab9de76e)
50
-
4. Lists & Keys — using `key` prop: [Commit Details]()
50
+
4. Lists & Keys — using `key` prop
51
+
1. The *incorrect* way: [Commit Details](https://github.com/Ch-sriram/react/commit/52235c6906dfd14d20103adb78378881ac2c6b2d)
52
+
2. The *correct* way \[making lists flexible\]: [Commit Details]()
Copy file name to clipboardExpand all lines: react-examples/src/App.js
+34-48Lines changed: 34 additions & 48 deletions
Original file line number
Diff line number
Diff line change
@@ -5,29 +5,49 @@ import { Person } from './components/person/person.component';
5
5
classAppextendsComponent{
6
6
state={
7
7
persons: [
8
-
{name: 'Ram',age: 28},
9
-
{name: 'Roop',age: 29},
10
-
{name: 'Max',age: 28}
8
+
{name: 'Ram',age: 28,id: 'a1'},
9
+
{name: 'Roop',age: 29,id: 'a2'},
10
+
{name: 'Max',age: 28,id: 'a3'}
11
11
],
12
12
otherState: "This has some value",
13
13
showPersons: false
14
14
}
15
15
16
-
nameChangedHandler=event=>{
17
-
this.setState({
18
-
persons: [
19
-
{id: 'abc1',name: "Mar",age: 82},
20
-
{id: 'abc2',name: event.target.value,age: 92},
21
-
{id: 'abc3',name: "Xam",age: 82},
22
-
],
23
-
});
16
+
nameChangedHandler=(event,id)=>{
17
+
// we'll only update the state of the person for which
18
+
// the input field was changed, and so, we first need to
19
+
// find the index of the Person where the input field was
20
+
// changed in the DOM
21
+
22
+
constpersonIndex=this.state.persons.findIndex(
23
+
person=>person.id===id
24
+
);
25
+
26
+
// We can get the person we want to be modified from the
27
+
// state now, but we DON'T want to get it the foll. way:
28
+
29
+
// const person = this.state.persons[personIndex]; // this will directly mutate the actual person in the state, which should not be done directly, only by the setState() method.
30
+
31
+
// In ES5:
32
+
// const person = Object.assign({}, this.state.persons[personIndex]);
0 commit comments