Search code examples
admin-on-restreact-admin

How to provide a custom redirect to a SimpleForm inside Edit?


After an edit, I follow the convention to redirect on the list.

However, I am in a case where I need to provide additional parameters in the list URL.

Is there a way to "cheat" on SimpleForm/Edit to customize the redirect ?

Here is the relevant part of my code :

const ProviderUserEditActions = ({ basePath, data }) => {
  if (! data) return null
  return (
    <CardActions style={cardActionStyle}>
        <ListButton basePath={basePath+'?'+queryString.stringify({providerId:data.providerId, providerName:data.providerName})} />
        <RefreshButton />
    </CardActions>
  );
}

export const ProviderUserEdit = (props) => (
    <Edit actions={<ProviderUserEditActions/>} title={<ProviderUserEditTitle />} {...props}>
        {/*redirect=false will cause the loosing of the URL parameters :(
          but this is the "least worst", because otherwise it would redirect to
          'list' but without URL parameters it will completely crash...
          we hope that someday we will be able to provide our own redirect
          function
          */}
        <SimpleForm redirect={false}>
            <TextInput source="name" />
            <TextInput source="email" />
            <TextInput source="phone" />
            <BooleanInput source="activated" />
        </SimpleForm>
    </Edit>
);

Solution

  • It's not documented, so I'll create an issue for adding this to the documentation, but you can specify a path as the redirect prop:

    <SimpleForm redirect={`/list?myParam=${myValue}`}>

    See: https://github.com/marmelab/react-admin/blob/master/packages/ra-core/src/util/resolveRedirectTo.js