herrero-tranquilo / RE

RE:* LOG 블로그 서비스만들기 ...ing
http://35.213.136.130/
0 stars 0 forks source link

TODO: #40

Open herrero-tranquilo opened 4 years ago

herrero-tranquilo commented 4 years ago

계속해서 변경 :sunglasses:

TODO

herrero-tranquilo commented 4 years ago

@reduxjs/toolkit 적용?

  - 사용중인 typesafe-actions와 비교를 좀 해보자

@reduxjs/toolkit

Reselect

미들웨어로는 thunk를 default로 하고있어 saga를 사용하는데 문제가 있는 것은 아니지만 몇몇 api들이 thunk와 잘 맞는듯 보인다.

configureStore

createSlice

import { createSlice, createAction, PayloadAction } from '@reduxjs/toolkit'
import { createStore, combineReducers } from 'redux'

const incrementBy = createAction<number>('incrementBy')
const decrementBy = createAction<number>('decrementBy')

const counter = createSlice({
  name: 'counter',
  initialState: 0 as number,
  reducers: {
    increment: state => state + 1,
    decrement: state => state - 1,
    multiply: {
      reducer: (state, action: PayloadAction<number>) => state * action.payload,
      prepare: (value: number) => ({ payload: value || 2 }) // fallback if the payload is a falsy value
    }
  },
  // "builder callback API", recommended for TypeScript users
  extraReducers: builder => {
    builder.addCase(incrementBy, (state, action) => {
      return state + action.payload
    })
    builder.addCase(decrementBy, (state, action) => {
      return state - action.payload
    })
  }
})

/*아래와 같은 사용이 가능*/
export const { increment, decrement, multiply } = counter.actions
store.dispatch(counter.actions.increment())
// -> { counter: 1, user: {name : '', age: 21} }

===========================================

const user = createSlice({
  name: 'user',
  initialState: { name: '', age: 20 },
  reducers: {
    setUserName: (state, action) => {
      state.name = action.payload // mutate the state all you want with immer
    }
  },
  // "map object API"
  extraReducers: {
    [counter.actions.increment]: (state, action) => {
      state.age += 1
    }
  }
})

==============================================

const reducer = combineReducers({
  counter: counter.reducer,
  user: user.reducer
})

const store = createStore(reducer)

https://github.com/reduxjs/redux-toolkit

typesafe-actions

https://github.com/piotrwitek/typesafe-actions

redux-observable과 redux-saga를 사용하는 api 사용 예시를 들고있다. async-flows

그외

typesafe-actions

// with "import as ..." import as todos from './actions'; export type TodosAction = ActionType; // TodosAction: { type: 'action1' } | { type: 'action2' } | { type: 'action3' }

// with nested action-creator map case const actions = { action1: createAction('action1'), nested: { action2: createAction('action2'), moreNested: { action3: createAction('action3'), }, }, }; export type RootAction = ActionType; // RootAction: { type: 'action1' } | { type: 'action2' } | { type: 'action3' }

헬퍼 타입을 만들어도 될 듯 하고 아니어도 아직 딱히 불편함을 느끼진 못한부분

```javascript
import { combineReducers } from 'redux';
import { StateType } from 'typesafe-actions';

// with reducer function
const todosReducer = (state: Todo[] = [], action: TodosAction) => {
  switch (action.type) {
    case getType(todos.add):
      return [...state, action.payload];
    ...
export type TodosState = StateType<typeof todosReducer>;

// with nested/combined reducers
const rootReducer = combineReducers({
  router: routerReducer,
  counters: countersReducer,
});
export type RootState = StateType<typeof rootReducer>;

이부분도 아직 불편한지 모르겠다.

herrero-tranquilo commented 4 years ago

usermenu 클릭으로 변경 commit 628e52dcf12ecc8c04760285331a64311503db83