Features a Todo list using useReducer and a counter increment button/display. The Todo List component is a child of the Counter component.
+
+
Count
+
{count}
+
+
+
+
+ )
+}
+
+export default CounterList
diff --git a/client/src/features/usereducer/components/counterlistmemo/index.js b/client/src/features/usereducer/components/counterlistmemo/index.js
new file mode 100644
index 0000000..08102b8
--- /dev/null
+++ b/client/src/features/usereducer/components/counterlistmemo/index.js
@@ -0,0 +1,26 @@
+import { useState } from 'react'
+import TodoListMemo from '../todolistmemo'
+
+// Optimize rerenders by wrapping TodoListMemo in a memo() function
+// Even it the containing parent's local states changes
+function CounterListMemo () {
+ const [count, setCount] = useState(0)
+ console.log('counter render')
+
+ return (
+
+
CounterListMemo
+
Features a Todo list using useReducer and a counter increment button/display. The Todo List component is a child of the Counter component. The TodoList component is wrapped by React.memo().
+
+
Count
+
{count}
+
+
+
+
+ )
+}
+
+export default CounterListMemo
diff --git a/client/src/features/usereducer/components/counterlistoptimized/index.js b/client/src/features/usereducer/components/counterlistoptimized/index.js
new file mode 100644
index 0000000..56e3364
--- /dev/null
+++ b/client/src/features/usereducer/components/counterlistoptimized/index.js
@@ -0,0 +1,17 @@
+import Counter from '../counter'
+import TodoList from '../todolist'
+
+// Optimized rendering by lowering the states
+function CounterListOptimized () {
+ return (
+
+
CounterListOptimized
+
Features a Todo list using useReducer and a counter increment button/display. The Todo List component and Counter component are children of a parent component.
+ )
+}
+
+export default memo(TodoListMemo)
diff --git a/client/src/features/usereducer/index.js b/client/src/features/usereducer/index.js
new file mode 100644
index 0000000..d82ac9b
--- /dev/null
+++ b/client/src/features/usereducer/index.js
@@ -0,0 +1,9 @@
+import CounterList from './components/counterlist'
+import CounterListOptimized from './components/counterlistoptimized'
+import CounterListMemo from './components/counterlistmemo'
+
+export {
+ CounterList,
+ CounterListOptimized,
+ CounterListMemo
+}
diff --git a/client/src/features/users/index.js b/client/src/features/users/index.js
new file mode 100644
index 0000000..7efd1df
--- /dev/null
+++ b/client/src/features/users/index.js
@@ -0,0 +1,7 @@
+import Users from './userscontainer'
+import UserList from './userlist'
+
+export {
+ Users,
+ UserList
+}
diff --git a/client/src/domain/users/userlist/index.js b/client/src/features/users/userlist/index.js
similarity index 82%
rename from client/src/domain/users/userlist/index.js
rename to client/src/features/users/userlist/index.js
index d9d2d07..f1bb8b4 100644
--- a/client/src/domain/users/userlist/index.js
+++ b/client/src/features/users/userlist/index.js
@@ -1,7 +1,7 @@
import PropTypes from 'prop-types'
import { useSelector } from 'react-redux'
-function UserListComponent ({ deleteUser }) {
+function UserList ({ deleteUser }) {
const {ids, entities: users } = useSelector(state => state.users)
return (
@@ -20,8 +20,8 @@ function UserListComponent ({ deleteUser }) {
)
}
-UserListComponent.propTypes = {
+UserList.propTypes = {
deleteUser: PropTypes.func
}
-export default UserListComponent
+export default UserList
diff --git a/client/src/features/users/userscontainer/index.js b/client/src/features/users/userscontainer/index.js
new file mode 100644
index 0000000..4585325
--- /dev/null
+++ b/client/src/features/users/userscontainer/index.js
@@ -0,0 +1,40 @@
+import { useDispatch } from 'react-redux'
+import { userReceived, userDelete } from '@/lib/store/users/usersSlice'
+import { todoDelete, todoReceived } from '@/lib/store/todos/todoSlice'
+
+import UsersComponent from './users'
+
+function Users () {
+ const dispatch = useDispatch()
+
+ const addUser = () => {
+ dispatch(userReceived({
+ text: 'User anonymous!'
+ }))
+ }
+
+ const deleteUser = (id) => {
+ dispatch(userDelete(id))
+ }
+
+ const addTodo = () => {
+ dispatch(todoReceived({
+ text: 'Hello, world!'
+ }))
+ }
+
+ const deleteTodo = (id) => {
+ dispatch(todoDelete(id))
+ }
+
+ return (
+
+ )
+}
+
+export default Users
diff --git a/client/src/components/users/index.js b/client/src/features/users/userscontainer/users.js
similarity index 83%
rename from client/src/components/users/index.js
rename to client/src/features/users/userscontainer/users.js
index d947340..4143637 100644
--- a/client/src/components/users/index.js
+++ b/client/src/features/users/userscontainer/users.js
@@ -1,8 +1,8 @@
import PropTypes from 'prop-types'
import Page from '@/common/layout/page'
import Card from '@/common/ui/card'
-import UserListComponent from '@/domain/users/userlist'
-import TodoListComponent from '@/domain/redux/todolist'
+import UserList from '../userlist'
+import { TodoListComponent } from '@/features/redux'
function UsersComponent ({
addUser,
@@ -34,7 +34,7 @@ function UsersComponent ({
-
+
@@ -58,7 +58,9 @@ function UsersComponent ({
UsersComponent.propTypes = {
addUser: PropTypes.func,
- deleteUser: PropTypes.func
+ deleteUser: PropTypes.func,
+ deleteTodo: PropTypes.func,
+ addTodo: PropTypes.func,
}
export default UsersComponent
diff --git a/client/src/domain/usestate/todolistfull/index.js b/client/src/features/usestate/components/todolistfull/index.js
similarity index 100%
rename from client/src/domain/usestate/todolistfull/index.js
rename to client/src/features/usestate/components/todolistfull/index.js
diff --git a/client/src/domain/usestate/todolist/index.js b/client/src/features/usestate/components/todolistv3/index.js
similarity index 100%
rename from client/src/domain/usestate/todolist/index.js
rename to client/src/features/usestate/components/todolistv3/index.js
diff --git a/client/src/features/usestate/index.js b/client/src/features/usestate/index.js
new file mode 100644
index 0000000..c04061b
--- /dev/null
+++ b/client/src/features/usestate/index.js
@@ -0,0 +1,9 @@
+import UseStateComponent from './usestatecomponent'
+import TodoListComponentV3 from './components/todolistv3'
+import TodoListComponentFull from './components/todolistfull'
+
+export {
+ UseStateComponent,
+ TodoListComponentFull,
+ TodoListComponentV3
+}
diff --git a/client/src/components/usestate/index.js b/client/src/features/usestate/usestatecomponent/index.js
similarity index 93%
rename from client/src/components/usestate/index.js
rename to client/src/features/usestate/usestatecomponent/index.js
index 9b7c353..16f4612 100644
--- a/client/src/components/usestate/index.js
+++ b/client/src/features/usestate/usestatecomponent/index.js
@@ -2,8 +2,8 @@ import { useState } from 'react'
import Card from '@/common/ui/card'
import Page from '@/common/layout/page'
-import TodoListComponentV3 from '@/domain/usestate/todolist'
-import TodoListComponentFull from '@/domain/usestate/todolistfull'
+import TodoListComponentV3 from '../components/todolistv3'
+import TodoListComponentFull from '../components/todolistfull'
function UseStateComponent () {
const [state, setState] = useState([])
diff --git a/client/src/domain/usesyncexternalstore/todolist/index.js b/client/src/features/usesyncexternalstore/components/todolistv2/index.js
similarity index 100%
rename from client/src/domain/usesyncexternalstore/todolist/index.js
rename to client/src/features/usesyncexternalstore/components/todolistv2/index.js
diff --git a/client/src/features/usesyncexternalstore/index.js b/client/src/features/usesyncexternalstore/index.js
new file mode 100644
index 0000000..91e0db9
--- /dev/null
+++ b/client/src/features/usesyncexternalstore/index.js
@@ -0,0 +1,7 @@
+import SyncExternalStore from './syncexternalstore'
+import TodoListComponentV2 from './components/todolistv2'
+
+export {
+ SyncExternalStore,
+ TodoListComponentV2
+}
diff --git a/client/src/features/usesyncexternalstore/syncexternalstore/index.js b/client/src/features/usesyncexternalstore/syncexternalstore/index.js
new file mode 100644
index 0000000..4f242fb
--- /dev/null
+++ b/client/src/features/usesyncexternalstore/syncexternalstore/index.js
@@ -0,0 +1,15 @@
+import SyncExternalStoreComponent from './syncexternalstore'
+import useTodos from '@/lib/hooks/usetodo'
+
+function SyncExternalStore () {
+ const { addTodo, deleteTodo } = useTodos()
+
+ return (
+
+ )
+}
+
+export default SyncExternalStore
diff --git a/client/src/components/usesyncexternalstore/index.js b/client/src/features/usesyncexternalstore/syncexternalstore/syncexternalstore.js
similarity index 82%
rename from client/src/components/usesyncexternalstore/index.js
rename to client/src/features/usesyncexternalstore/syncexternalstore/syncexternalstore.js
index 5e7687b..b4ccd39 100644
--- a/client/src/components/usesyncexternalstore/index.js
+++ b/client/src/features/usesyncexternalstore/syncexternalstore/syncexternalstore.js
@@ -1,9 +1,9 @@
import PropTypes from 'prop-types'
import Page from '@/common/layout/page'
import Card from '@/common/ui/card'
-import TodoListComponentV2 from '@/domain/usesyncexternalstore/todolist'
+import TodoListComponentV2 from '../components/todolistv2'
-function UseSyncExternalStoreComponent ({
+function SyncExternalStoreComponent ({
addTodo,
deleteTodo
}) {
@@ -49,9 +49,9 @@ function UseSyncExternalStoreComponent ({
)
}
-UseSyncExternalStoreComponent.propTypes = {
+SyncExternalStoreComponent.propTypes = {
addTodo: PropTypes.func,
deleteTodo: PropTypes.func
}
-export default UseSyncExternalStoreComponent
+export default SyncExternalStoreComponent
diff --git a/client/src/pages/_app.js b/client/src/pages/_app.js
index bf9ec3f..22ea4e6 100644
--- a/client/src/pages/_app.js
+++ b/client/src/pages/_app.js
@@ -1,12 +1,20 @@
+import PropTypes from 'prop-types'
import { Provider } from 'react-redux'
import { store } from '@/store/store'
import '@/styles/globals.css'
-export default function App({ Component, pageProps }) {
+function App({ Component, pageProps }) {
return (
)
}
+
+App.propTypes = {
+ Component: PropTypes.func,
+ pageProps: PropTypes.object
+}
+
+export default App
diff --git a/client/src/pages/redux/index.js b/client/src/pages/redux/index.js
index d25077b..f40f497 100644
--- a/client/src/pages/redux/index.js
+++ b/client/src/pages/redux/index.js
@@ -1,27 +1,9 @@
-import { useDispatch } from 'react-redux'
-import { todoReceived, todoDelete } from '@/lib/store/todos/todoSlice'
-
-import ReduxComponent from '@/components/redux'
-
-function ReduxContainer () {
- const dispatch = useDispatch()
-
- const addTodo = () => {
- dispatch(todoReceived({
- text: 'Hello, world!'
- }))
- }
-
- const deleteTodo = (id) => {
- dispatch(todoDelete(id))
- }
+import { Redux } from '@/features/redux'
+function ReduxPage () {
return (
-
+
)
}
-export default ReduxContainer
+export default ReduxPage
diff --git a/client/src/pages/usereducer/counterlist.js b/client/src/pages/usereducer/counterlist.js
new file mode 100644
index 0000000..448b8a0
--- /dev/null
+++ b/client/src/pages/usereducer/counterlist.js
@@ -0,0 +1,9 @@
+import { CounterList } from '@/features/usereducer'
+
+function CounterListPage () {
+ return (
+
+ )
+}
+
+export default CounterListPage
diff --git a/client/src/pages/usereducer/counterlistmemo.js b/client/src/pages/usereducer/counterlistmemo.js
new file mode 100644
index 0000000..98e81c1
--- /dev/null
+++ b/client/src/pages/usereducer/counterlistmemo.js
@@ -0,0 +1,9 @@
+import { CounterListMemo } from '@/features/usereducer'
+
+function CounterListMemoPage () {
+ return (
+
+ )
+}
+
+export default CounterListMemoPage
diff --git a/client/src/pages/usereducer/counterlistoptimized.js b/client/src/pages/usereducer/counterlistoptimized.js
new file mode 100644
index 0000000..43d8132
--- /dev/null
+++ b/client/src/pages/usereducer/counterlistoptimized.js
@@ -0,0 +1,9 @@
+import { CounterListOptimized } from '@/features/usereducer'
+
+function CounterListOptimizedPage () {
+ return (
+
+ )
+}
+
+export default CounterListOptimizedPage
diff --git a/client/src/pages/usereducer/index.js b/client/src/pages/usereducer/index.js
new file mode 100644
index 0000000..c6110bc
--- /dev/null
+++ b/client/src/pages/usereducer/index.js
@@ -0,0 +1,37 @@
+import Link from 'next/link'
+
+function ReduxPage () {
+ return (
+
+
useReducer
+
Checking out useReducer's basic useage and several re-render optimization techniques.
+
+
+
+
+
+
+
CounterList
+
Features a Todo list using useReducer and a counter increment button/display. The Todo List component is a child of the Counter component.
+
+
+
+
+
+
CounterListOptimized
+
Features a Todo list using useReducer and a counter increment button/display. The Todo List component and Counter component are children of a parent component.
+
+
+
+
+
+
CounterListMemo
+
Features a Todo list using useReducer and a counter increment button/display. The Todo List component is a child of the Counter component. The TodoList component is wrapped by React.memo().