Skip to content

Commit

Permalink
any타입 제거 및 useState도 만들어보기(실패)
Browse files Browse the repository at this point in the history
  • Loading branch information
BongjoonKim committed Jan 2, 2025
1 parent 00a56d8 commit 179da75
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/@lib/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./useDeepMemo";
export * from "./useMemo";
export * from "./useCallback";
export * from "./useRef";
export * from "./useState";
2 changes: 1 addition & 1 deletion src/@lib/hooks/useRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import {useState} from "react";

export function useRef<T>(initialValue: T): { current: T } {
// React의 useState를 이용해서 만들어보세요.
const [data, setData] = useState<any>({current : initialValue});
const [data, setData] = useState<T>({current : initialValue});
return data;
}
37 changes: 37 additions & 0 deletions src/@lib/hooks/useState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';

// 이것저것 수정해서 구현해보려다 실패
type SetStateAction<T> = T | ((prevState: T) => T);
type Dispatch<T> = (action: SetStateAction<T>) => void;

const states: unknown[] = [];
let currentStateIndex = 0;

export function useState<T>(initialValue: T): [T, Dispatch<T>] {
const index = currentStateIndex;

if (states[index] === undefined) {
states[index] = initialValue;
}

const setState = (newValue: SetStateAction<T>) => {
const value = typeof newValue === 'function'
? (newValue as (prevState: T) => T)(states[index] as T)
: newValue;

if (states[index] !== value) {
states[index] = value;
React.Component.prototype.forceUpdate.call(this);
}
};

currentStateIndex++;

React.useEffect(() => {
return () => {
currentStateIndex = 0;
};
});

return [states[index] as T, setState];
}
10 changes: 4 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React, {useState, createContext, useContext, useEffect} from "react";
import React, {useState, createContext, useContext } from "react";
import { generateItems, renderLog } from "./utils";
import {memo, useCallback, useMemo } from "./@lib";
// import {memo} from "react";
import {deepEquals} from "./@lib";

// 타입 정의
interface Item {
Expand Down Expand Up @@ -37,9 +35,9 @@ interface AppContextType {
}

const AppContext = createContext<AppContextType | undefined>(undefined);
const ThemeContext = createContext<any>(undefined);
const AuthContext = createContext<any>(undefined);
const NotificationContext = createContext<any>(undefined);
const ThemeContext = createContext<string | undefined>(undefined);
const AuthContext = createContext<User | undefined>(undefined);
const NotificationContext = createContext<Notification | undefined>(undefined);

// 커스텀 훅들
const useTheme = () => {
Expand Down

0 comments on commit 179da75

Please sign in to comment.