1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| import React, { createContext, Dispatch, useContext, useReducer } from 'react'; import { Comment } from '../types';
type State = Map<number, Comment>;
const initialState: State = new Map<number, Comment>();
initialState.set(0, { id: 0, avatar: 'https://i.pravatar.cc/48', username: 'string', publishedTime: '1주 전', content: 'hello world!', likeCount: 999, dislikeCount: 0, });
type CommentAction = { type: 'ADD_COMMENT'; comment: Comment };
const CommentContext = createContext<State>(initialState); const CommentDispatch = createContext<Dispatch<CommentAction>>(() => null);
let nextId = 0;
function reducer(state: State, action: CommentAction): State { switch (action.type) { case 'ADD_COMMENT': const { comment } = action; nextId += 1; return new Map(state).set(nextId, { ...comment, id: nextId });
default: throw new Error('Unhandled action'); } }
export default function GlobalContextProvider({ children, }: { children: React.ReactNode; }) { const [state, dispatch] = useReducer(reducer, initialState);
return ( <CommentContext.Provider value={state}> <CommentDispatch.Provider value={dispatch}> {children} </CommentDispatch.Provider> </CommentContext.Provider> ); }
export function useCommentState() { return useContext(CommentContext); }
export function useCommentDispatch() { return useContext(CommentDispatch); }
|