Skip to content

Commit 32b2bcb

Browse files
committed
more examples
1 parent 5e5b94b commit 32b2bcb

File tree

6 files changed

+2029
-1214
lines changed

6 files changed

+2029
-1214
lines changed

package-lock.json

Lines changed: 1988 additions & 1202 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"react": "^16.8.6",
7-
"react-dom": "^16.8.6",
8-
"react-scripts": "3.1.0"
6+
"react": "^16.11.0",
7+
"react-dom": "^16.11.0",
8+
"react-scripts": "3.2.0"
99
},
1010
"scripts": {
1111
"start": "react-scripts start",

src/App.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import ListWithAddItem from './components/ListWithAddItem';
77
import ListWithUpdateItem from './components/ListWithUpdateItem';
88
import ListWithRemoveItem from './components/ListWithRemoveItem';
99
import NestedList from './components/NestedList';
10-
import ListWithoutKey from './components/ListWithoutKey';
10+
import { ListWithUnstableIndex,
11+
ListWithStableIndex } from './components/ListWithSort';
1112
import ListScrollToItem from './components/ListScrollToItem';
1213
import ListScrollToItemOutside from './components/ListScrollToItemOutside';
1314

@@ -33,8 +34,11 @@ const App = () => (
3334
<h2>"List with remove Item"-Examples</h2>
3435
<ListWithRemoveItem />
3536

36-
<h2>"List without Key and thus Bug"-Example</h2>
37-
<ListWithoutKey />
37+
<h2>"List with unstable Key and thus Bug"-Example</h2>
38+
<ListWithUnstableIndex />
39+
40+
<h2>"List with stable Key"-Example</h2>
41+
<ListWithStableIndex />
3842

3943
<h2>"List with nested List"-Example</h2>
4044
<NestedList />

src/components/ListScrollToItem.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { createRef } from 'react';
1+
import React from 'react';
22

33
const list = [
44
{
@@ -30,7 +30,7 @@ const list = [
3030
const ListScrollToItem = () => (
3131
<ul>
3232
{list.map(item => {
33-
const ref = createRef();
33+
const ref = React.createRef();
3434

3535
// Scroll Into View API: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
3636
const handleClick = () =>

src/components/ListScrollToItemOutside.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { createRef } from 'react';
1+
import React from 'react';
22

33
const list = [
44
{
@@ -29,7 +29,7 @@ const list = [
2929

3030
const ListScrollToItem = () => {
3131
const refs = list.reduce((acc, value) => {
32-
acc[value.id] = createRef();
32+
acc[value.id] = React.createRef();
3333
return acc;
3434
}, {});
3535

src/components/ListWithoutKey.js renamed to src/components/ListWithSort.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const initialList = ['Learn React', 'Learn GraphQL'];
2929
// );
3030
// };
3131

32-
const ListWithUnstableIndex = () => {
32+
export const ListWithUnstableIndex = () => {
3333
const [list, setList] = React.useState(initialList);
3434

3535
const handleClick = event => {
@@ -56,4 +56,29 @@ const ListWithUnstableIndex = () => {
5656
);
5757
};
5858

59-
export default ListWithUnstableIndex;
59+
export const ListWithStableIndex = () => {
60+
const [list, setList] = React.useState(initialList);
61+
62+
const handleClick = event => {
63+
setList(list.slice().reverse());
64+
};
65+
66+
return (
67+
<div>
68+
<ul>
69+
{list.map((item, index) => (
70+
<li key={item}>
71+
<label>
72+
<input type="checkbox" />
73+
{item}
74+
</label>
75+
</li>
76+
))}
77+
</ul>
78+
79+
<button type="button" onClick={handleClick}>
80+
Reverse List
81+
</button>
82+
</div>
83+
);
84+
};

0 commit comments

Comments
 (0)