0

我有一个自定义钩子,我正试图将其移入上下文/减速器模式:

import { sortDateStrings } from '@/Utils/SortDateStrings'


    function useDiscounts ({ data }) {
      const [effectiveDates] = useState(data.effectiveDates || [])
            
      const sortedEffectiveDates = sortDateStrings(effectiveDates, { desc: true })
            
      const effectiveDateOptions = sortedEffectiveDates.map(item => ({ id: item, value: item, label: dayjs(item).format(DATE_FORMAT) }))
    
      return {
        effectiveDateOptions
      }
    }

And this is the beginning of `initialState` for the useReducer pattern:

    const initialState = {
      effectiveDates: [],
      sortedEffectiveDates: sortDateStrings(this.effectiveDates, { desc: true }),
    }

你可以看到我有一个属性,initialState它正在调用一个函数this

我在想你应该做这样的事情:

function DiscountsReducer (state, action) {
  switch (action.type) {
    case 'effectiveDateOptions': {
     return { ...state, ...{ // but not sure what to do here? }
    }
   
    default: {
      throw new Error(`Unhandled action type: ${action.type}`)
    }
  }
}

任何人都可以建议使用这种模式来处理这个问题的方法是什么?

4

0 回答 0