Skip to content

Commit 679fe7e

Browse files
committed
chore: Minimize full page rerendering
1 parent be5b7d7 commit 679fe7e

File tree

8 files changed

+67
-17
lines changed

8 files changed

+67
-17
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.card {
2+
border: 1px solid;
3+
max-width: 400px;
4+
min-height: 100px;
5+
border-radius: 16px;
6+
padding: 16px;
7+
}

client/src/common/ui/card/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import styles from './Card.module.css'
2+
3+
function Card ({ children }) {
4+
return (
5+
<div className={styles.card}>
6+
{children}
7+
</div>
8+
)
9+
}
10+
11+
export default Card
File renamed without changes.

client/src/components/home/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import Link from 'next/link'
22
import Page from '@/common/layout/page'
3+
34
import { Inter } from 'next/font/google'
4-
import styles from '@/styles/Home.module.css'
5+
import styles from './Home.module.css'
56
import navlinks from './items.json'
67

78
const inter = Inter({ subsets: ['latin'] })
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import PropTypes from 'prop-types'
22
import Page from '@/common/layout/page'
3+
import Card from '@/common/ui/card'
4+
import TodoListComponent from '@/domain/redux/todolist'
35

46
function ReduxComponent ({
5-
todos,
67
addTodo,
78
deleteTodo
89
}) {
@@ -17,20 +18,19 @@ function ReduxComponent ({
1718
Add Todo
1819
</button>
1920

20-
<ul style={{ marginTop: '24px' }}>
21-
{(todos).map(((item, id) => (
22-
<li key={id}>
23-
<span>id: {item.id}, {item.text}</span>
24-
<span>
25-
<button onClick={() => deleteTodo(item.id)}>
26-
[ x ]
27-
</button>
28-
</span>
29-
</li>
30-
)))}
31-
</ul>
21+
<TodoListComponent deleteTodo={deleteTodo} />
22+
<br />
23+
24+
<Card>
25+
hello
26+
</Card>
3227
</Page>
3328
)
3429
}
3530

31+
ReduxComponent.propTypes = {
32+
addTodo: PropTypes.func,
33+
deleteTodo: PropTypes.func
34+
}
35+
3636
export default ReduxComponent
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import PropTypes from 'prop-types'
2+
import { useSelector } from 'react-redux'
3+
4+
function TodoListComponent ({ deleteTodo }) {
5+
const {ids, entities: todos } = useSelector(state => state.todos)
6+
7+
return (
8+
<ul style={{ marginTop: '24px' }}>
9+
{(ids).map(((id, index) => (
10+
<li key={index}>
11+
<span>id: {todos[id].id}, {todos[id].text}</span>
12+
<span>
13+
<button onClick={() => deleteTodo(id)}>
14+
[ x ]
15+
</button>
16+
</span>
17+
</li>
18+
)))}
19+
</ul>
20+
)
21+
}
22+
23+
TodoListComponent.propTypes = {
24+
deleteTodo: PropTypes.func
25+
}
26+
27+
export default TodoListComponent

client/src/lib/store/todos/todoSlice.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Notes:
2+
// https://redux.js.org/tutorials/essentials/part-6-performance-normalization#normalized-state-structure
3+
// https://redux.js.org/tutorials/essentials/part-6-performance-normalization#optimizing-the-posts-list
4+
15
import {
26
createSlice,
37
createEntityAdapter
@@ -8,10 +12,12 @@ const STATES = {
812
PENDING: 'pending'
913
}
1014

15+
// Entiti adapter
1116
const todosAdapter = createEntityAdapter({
1217
selectId: (todo) => todo.id
1318
})
1419

20+
// Slice
1521
const todoSlice = createSlice({
1622
name: 'todos',
1723
initialState: todosAdapter.getInitialState({

client/src/pages/redux/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { useSelector, useDispatch } from 'react-redux'
1+
import { useDispatch } from 'react-redux'
22
import { todoReceived, todoDelete } from '@/lib/store/todos/todoSlice'
33

44
import ReduxComponent from '@/components/redux'
55

66
function ReduxContainer () {
77
const dispatch = useDispatch()
8-
const todos = useSelector(state => state.todos)
98

109
const addTodo = () => {
1110
dispatch(todoReceived({
@@ -19,7 +18,6 @@ function ReduxContainer () {
1918

2019
return (
2120
<ReduxComponent
22-
todos={todos.ids.map(id => todos.entities[id])}
2321
addTodo={addTodo}
2422
deleteTodo={deleteTodo}
2523
/>

0 commit comments

Comments
 (0)