Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt number of useMutation() instances to the number of action arguments #1

Open
gabriel-frattini opened this issue Aug 1, 2022 · 0 comments

Comments

@gabriel-frattini
Copy link
Owner

gabriel-frattini commented Aug 1, 2022

Currently the useTrpcReducer hook takes in a arbitrary amount of actions

  actions: {
      arg_0: TMutationPath
      arg_1?: TMutationPath
      arg_2?: TMutationPath
      arg_3?: TMutationPath
      arg_4?: TMutationPath
    },

The reason for this is because the useMutation() hook can't be called conditionally or in a function (rules of hooks) & hence it seems to me like i need to define the number of actions in advance in order to call the mutation instances like so

const firstProcedureMutation = useMutation(actions.arg_0)
const secondProcedureMutation = useMutation(actions.arg_1 ? actions.arg_1 : [''])
const thirdProcedureMutation = useMutation(actions.arg_2 ? actions.arg_2 : [''])
const fourthProcedureMutation = useMutation(actions.arg_3 ? actions.arg_3 : [''])
const fifthProcedureMutation = useMutation(actions.arg_4 ? actions.arg_4 : [''])

This is obsviously really ugly and was just a quick solutation at first, but after a week im still stuck at this solution because I can't figure out how to adapt the number of useMutation() instances to the number of action arguments

This also leads to a dispatch function that looks like this

 function dispatch(action: TDispatch<TRouter>, opts?: TOpts) {
      const { type } = action
      if (type[0] === actions.arg_0[0]) {
        updateState({ mutation: firstProcedureMutation, action, opts })
      }
      if (actions.arg_1 && type[0] === actions.arg_1[0]) {
        updateState({ mutation: secondProcedureMutation, action, opts })
      }
      if (actions.arg_2 && type[0] === actions.arg_2[0]) {
        updateState({ mutation: thirdProcedureMutation, action, opts })
      }
      if (actions.arg_3 && type[0] === actions.arg_3[0]) {
        updateState({ mutation: fourthProcedureMutation, action, opts })
      }
      if (actions.arg_4 && type[0] === actions.arg_4[0]) {
        updateState({ mutation: fifthProcedureMutation, action, opts })
      }
    }
    return { state: procedureQuery, dispatch }
  }

I think that the if statement is neccessary in order to supply the right action to the updateState function but again, I need some type of for-loop/mapping instead of these hardcoded mutations so that you can dynamically input any number of actions to useTrpcReducer and call updateState for every action argument.

Do we have any ideas on how to go about this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant