React Router/7/Errorfirst Mutation Reorder

/icons/calendar.svg

Last update

Oct 7, 2024

This transformation restructures action functions to perform error checking before data mutations. This change aligns with the new behavior in v7 where loaders no longer revalidate by default after an action throws or returns a Response with a 4xx/5xx status code.

Before

async function action() {
await mutateSomeData();
if (detectError()) {
throw new Response(error, { status: 400 });
}
await mutateOtherData();
// ...
}

After

async function action() {
if (detectError()) {
throw new Response(error, { status: 400 });
}
// All data is now mutated after validations
await mutateSomeData();
await mutateOtherData();
}

Build custom codemods

Use AI-powered codemod studio and automate undifferentiated tasks for yourself, colleagues or the community

background illustrationGet Started Now