Skip to content

Commit

Permalink
💩 shit: Made the tasks consistent disabling SSR, can't figure out how…
Browse files Browse the repository at this point in the history
… to useLocalStorage with a useReducer without overriding the local, however this should be erased when the login + database is implemented.
  • Loading branch information
bytevictor committed Mar 28, 2024
1 parent 80c7d37 commit 2e65bcb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
12 changes: 8 additions & 4 deletions app/Components/TasksApp/TaskApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import { useEffect, useReducer } from "react";
import AddTask from "./AddTask";
import TaskList from "./TaskList";
import useLocalStorageReducer from "@/app/_lib/customHooks/useLocalStorageWithReducer";
import { v4 as uuid } from "uuid";

export default function TaskApp() {
//const [tasks, setTasks] = useLocalStorage("tasks", []);
const [tasks, dispatch] = useLocalStorageReducer(tasksReducer, "tasks", initialTasks);
const [tasks, dispatch] = useLocalStorageReducer(
tasksReducer,
"tasks",
initialTasks
);

useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
Expand All @@ -26,7 +31,7 @@ export default function TaskApp() {
function handleAddTask(text: any) {
dispatch({
type: "added",
id: nextId++,
id: uuid(),
text: text,
isNew: true,
});
Expand Down Expand Up @@ -76,7 +81,7 @@ function tasksReducer(tasks: Array<any>, action: any) {
case "changed": {
return tasks.map((t: any) => {
if (t.id === action.task.id) {
return {...action.task, isNew: false};
return { ...action.task, isNew: false };
} else {
return t;
}
Expand All @@ -91,7 +96,6 @@ function tasksReducer(tasks: Array<any>, action: any) {
}
}

let nextId = 3;
const initialTasks = [
{ id: 0, text: "Philosopher’s Path", done: true, isNew: false },
{ id: 1, text: "Visit the temple", done: false, isNew: false },
Expand Down
6 changes: 5 additions & 1 deletion app/_lib/customHooks/useLocalStorageWithReducer.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Reducer, ReducerWithoutAction, useEffect, useReducer } from "react";
import useLocalStorage from "./useLocalStorageV2";

export default function useLocalStorageReducer(
reducer: Reducer<any, any>,
key: string,
initialState: Array<any>
) {
const [savedState, setSavedState] = useLocalStorage(key, initialState);

const [state, dispatch] = useReducer(reducer, initialState, () => {
const persisted = localStorage.getItem(key);
const persisted = localStorage?.getItem(key);

return persisted ? JSON.parse(persisted) : initialState;
});

Expand Down
15 changes: 11 additions & 4 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import Image from "next/image";
import { SoundButton } from "./Components/TasksApp/SoundButtonTest";
import TaskApp from "./Components/TasksApp/TaskApp";
import dynamic from "next/dynamic";


//Using this just to be able to use localStorage is probably against the Geneva convention
//but if I spend any more hours trying to figure out how to use localStorage
//in Next.js with a useReducer hook I will kill myself
const TaskAppNoSSR = dynamic(() => import("./Components/TasksApp/TaskApp"), {

This comment has been minimized.

Copy link
@somecodingwitch

somecodingwitch Mar 28, 2024

what does that even mean

This comment has been minimized.

Copy link
@bytevictor

bytevictor Mar 29, 2024

Author Owner

The state of the tasks is handled with an useReducer hook, the problem is that I can't find a way that, when combining the useReducer with the useLocalStorage (or using localStorage.get/set() directly), the original content of the localStorage is not overwritten. When I get it not to overwrite, I get either a hydration error or a 'localStorage is not defined' error.
Disabling SSR (Server Side Rendering) avoids this localStorage problem, but it is not an elegant solution.
Anyway this code will be rewritten when I implement the saving of tasks in the external database with an API.

Frustration makes me be too sincere when commenting on code hahaha

But, in my defense, science is on my side:
image

ssr: false,
});


export default function Home() {
return (
<main className="flex min-h-screen flex-col items-center lg:pt-0 lg:p-24 md:p-0 pt-0">
<TaskApp />
<TaskAppNoSSR />
</main>
);
}

0 comments on commit 2e65bcb

Please sign in to comment.