From ba302f76e72213776fbbb0bb7965f125a7aa7989 Mon Sep 17 00:00:00 2001
From: Yad Smood <1415488+ysmood@users.noreply.github.com>
Date: Thu, 10 Oct 2024 14:08:49 +0800
Subject: [PATCH] modulize the TodoApp
---
README.md | 5 +-
examples/TodoApp/Actions.tsx | 35 ++++++++
examples/TodoApp/Filter.tsx | 12 +++
examples/TodoApp/Title.tsx | 5 ++
examples/TodoApp/TodoItem.tsx | 24 ++++++
examples/TodoApp/TodoList.tsx | 13 +++
examples/TodoApp/ToggleAll.tsx | 14 +++
examples/TodoApp/index.tsx | 94 ++-------------------
examples/TodoApp/store.ts | 136 ------------------------------
examples/TodoApp/todos/actions.ts | 42 +++++++++
examples/TodoApp/todos/filter.ts | 13 +++
examples/TodoApp/todos/index.ts | 86 +++++++++++++++++++
examples/index.tsx | 20 ++++-
examples/utils.ts | 4 +
examples/utils.tsx | 14 ---
vitest.config.ts | 1 +
16 files changed, 274 insertions(+), 244 deletions(-)
create mode 100644 examples/TodoApp/Actions.tsx
create mode 100644 examples/TodoApp/Filter.tsx
create mode 100644 examples/TodoApp/Title.tsx
create mode 100644 examples/TodoApp/TodoItem.tsx
create mode 100644 examples/TodoApp/TodoList.tsx
create mode 100644 examples/TodoApp/ToggleAll.tsx
delete mode 100644 examples/TodoApp/store.ts
create mode 100644 examples/TodoApp/todos/actions.ts
create mode 100644 examples/TodoApp/todos/filter.ts
create mode 100644 examples/TodoApp/todos/index.ts
create mode 100644 examples/utils.ts
delete mode 100644 examples/utils.tsx
diff --git a/README.md b/README.md
index 3c6705a..d9d356c 100644
--- a/README.md
+++ b/README.md
@@ -7,7 +7,7 @@ An elegant state management solution for React.
- **Non-opinionated**: Like useState, only one core function, others are built on top of it.
- **Type safe**: The state is type safe and the return value is intuitive.
- **Global**: The state is global, you can access it anywhere.
-- **Scalable**: Supports selector to avoid unnecessary re-render.
+- **Scalable**: Naturally scale large state into multiple modules and files without performance degradation.
- **Tiny**: Less than [0.3KB](https://bundlephobia.com/package/create-global-state).
## Documentation
@@ -44,4 +44,5 @@ Its implementation is not type safe and the return value is not intuitive. It's
> Why not [zustand](https://github.com/pmndrs/zustand)?
-The typescript support is not good enough, the API is not intuitive. `create-global-state` is more like `useState` which aligns with the react API style. Check the [comparison](https://github.com/ysmood/create-global-state/issues/1).
+The typescript support is not good enough, the API is not intuitive. `create-global-state` is more like `useState` which aligns with the react API style. Check the [comparison](https://github.com/ysmood/create-global-state/issues/1). Zustand [Slices Pattern](https://zustand.docs.pmnd.rs/guides/slices-pattern) can cause naming conflict issues.
+`create-global-state` can naturally scale states by modules and files.
diff --git a/examples/TodoApp/Actions.tsx b/examples/TodoApp/Actions.tsx
new file mode 100644
index 0000000..4f3262e
--- /dev/null
+++ b/examples/TodoApp/Actions.tsx
@@ -0,0 +1,35 @@
+import Filter from "./Filter";
+import { addTodo, clearCompleted } from "./todos/actions";
+import { useZeroDone } from "./todos";
+import ToggleAll from "./ToggleAll";
+
+export default function Actions() {
+ return (
+
+
+
+
+
+
+ );
+}
+
+function AddTodo() {
+ return (
+
+ );
+}
+
+function ClearCompleted() {
+ return (
+
+ );
+}
diff --git a/examples/TodoApp/Filter.tsx b/examples/TodoApp/Filter.tsx
new file mode 100644
index 0000000..a80ad3c
--- /dev/null
+++ b/examples/TodoApp/Filter.tsx
@@ -0,0 +1,12 @@
+import { filters, setFilter } from "./todos/filter";
+
+// The component to filter the todos.
+export default function Filter() {
+ return (
+
+ );
+}
diff --git a/examples/TodoApp/Title.tsx b/examples/TodoApp/Title.tsx
new file mode 100644
index 0000000..6e754a4
--- /dev/null
+++ b/examples/TodoApp/Title.tsx
@@ -0,0 +1,5 @@
+import { useLeftCount } from "./todos";
+
+export default function Title() {
+ return
Todo App ({useLeftCount()} todos left)
;
+}
diff --git a/examples/TodoApp/TodoItem.tsx b/examples/TodoApp/TodoItem.tsx
new file mode 100644
index 0000000..452fedd
--- /dev/null
+++ b/examples/TodoApp/TodoItem.tsx
@@ -0,0 +1,24 @@
+import { delTodo, toggleTodo, updateTodo, useTodo } from "./todos";
+
+// The component to display a todo.
+export default function TodoItem({ id }: { id: number }) {
+ const { done, text } = useTodo(id);
+
+ return (
+