Skip to content

Commit

Permalink
feat: recoil 템플릿 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
cjeongmin committed Mar 18, 2024
1 parent e2b8353 commit 97656ce
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 29 deletions.
1 change: 0 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
"allow": ["private-constructors"]
}
],
"@typescript-eslint/no-unused-vars": "warn",
"react/jsx-no-useless-fragment": "off",
"react/react-in-jsx-scope": "off",
"react/jsx-filename-extension": [1, { "extensions": [".tsx", ".jsx"] }],
Expand Down
13 changes: 7 additions & 6 deletions src/features/auth/auth.action.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { useCallback, useMemo } from 'react';
import { useRecoilState } from 'recoil';

import { useAuthState } from '.';
import { authState } from './auth.atom';

export const useAuthActions = () => {
const [authState, setAuthState] = useAuthState();
const [, setAuth] = useRecoilState(authState);

const login = useCallback(() => {
setAuthState(prev => ({ ...prev, isLogin: true }));
}, []);
setAuth(prev => ({ ...prev, isLogin: true }));
}, [setAuth]);
const logout = useCallback(() => {
setAuthState(prev => ({ ...prev, isLogin: false }));
}, []);
setAuth(prev => ({ ...prev, isLogin: false }));
}, [setAuth]);

useMemo(
() => ({
Expand Down
11 changes: 11 additions & 0 deletions src/features/auth/auth.atom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { atom } from 'recoil';

import { type AuthState } from './auth.model';

export const authState = atom<AuthState>({
key: 'authState',
default: {
isLogin: false,
token: undefined,
},
});
3 changes: 2 additions & 1 deletion src/features/auth/auth.hook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useRecoilState, useRecoilValue } from 'recoil';

import { authState, getIsLogin } from './auth.model';
import { authState } from './auth.atom';
import { getIsLogin } from './auth.selector';

export const useAuthState = () => useRecoilState(authState);
export const useAuthIsLogin = () => useRecoilValue(getIsLogin);
18 changes: 0 additions & 18 deletions src/features/auth/auth.model.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
import { atom, selector } from 'recoil';

export interface AuthState {
isLogin: boolean;
token?: string;
}

export const authState = atom<AuthState>({
key: 'authState',
default: {
isLogin: false,
token: undefined,
},
});

export const getIsLogin = selector<boolean>({
key: 'authState/isLogin',
get: ({ get }) => {
const auth = get(authState);
return auth.isLogin;
},
});
11 changes: 11 additions & 0 deletions src/features/auth/auth.selector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { selector } from 'recoil';

import { authState } from './auth.atom';

export const getIsLogin = selector<boolean>({
key: 'authState/isLogin',
get: ({ get }) => {
const auth = get(authState);
return auth.isLogin;
},
});
8 changes: 5 additions & 3 deletions src/features/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { type AuthState, authState, getIsLogin } from './auth.model';
export { useAuthState, useAuthIsLogin } from './auth.hook';
export {} from './auth.action';
export * from './auth.action';
export * from './auth.atom';
export * from './auth.hook';
export * from './auth.model';
export * from './auth.selector';

0 comments on commit 97656ce

Please sign in to comment.