0

I have a custom hook which I am trying to move into the context/reducer pattern:

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 }),
    }

You can see I have a property on the initialState which is calling a function using this

I am thinking you should do something like 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}`)
    }
  }
}

Can anyone advise what is the way to handle this in using this pattern?

Antonio Pavicevac-Ortiz
  • 7,239
  • 17
  • 68
  • 141

0 Answers0