Multiple Actions With Separate Disjoint States #8064
-
I recently picked up Remix in order to build a Shopify Plugin. Essentially the user can pick some combination of settings which gradually refines various data in Shopify as the options are getting picked. Once the data is refined to a "view", the user can do one of many actions such as export, save locally to the app database, send to an external system, create a customer segment etc. So the
After doing some research on this, I stumbled upon these two: https://stackoverflow.com/questions/71123120/remix-usesubmit-arbitrary-data I am not entirely opposed to having the logic form inside the So if I have something like
The hacky solution to this is to unify the result types and have
but I don't like this approach very much. From my basic understanding, this seems to be in conflict with the architecture remix targets but is there essentially a way for me to have multiple action functions and store the results separately ? So that performing one action wouldn't overwrite the result of the other If the answer is "you are structuring your code incorrectly", that's fair. I would appreciate any guidance to get in the correct mindset. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
First, I also think multiple actions should be supported with a more intuitive solution. But answering your question: export const action = ({ request }: DataFunctionArgs) => {
const formData = await request.formData()
if (formData.get('intent') === 'do-something') {
return { outcome: 'did-something' }
} else if (formData.get('intent') === 'do-something-else') {
return { outcome: 'did-something-else' }
} else {
throw new Error(`Unknown intent: ${intent}`)
}
}
export default function Component() {
const doSomethingFetcher = useFetcher() as Extract<ReturnType<typeof action>, { outcome: 'did-something' }>
const doSomethingElseFetcher = useFetcher() as Extract<ReturnType<typeof action>, { outcome: 'did-something-else' }>
// ...
} If you feel there's one action that's the "main action" of that route, then use |
Beta Was this translation helpful? Give feedback.
You can still use a loader from a resource route. The difference between a loader and an action is really just the method. A loader handles GET requests.
You can build a separate resource route for each thing if keeping them together in a single loader/action would be messy.