

In this blog, we're diving into a powerful technique using Payload NextJS that can significantly enhance user experience by pre-filling values in forms. This approach not only saves time but also streamlines the document creation process, making it more efficient and user-friendly.
Elevate your Payload CMS experience with our guide to pre-filling values. This document supports our detailed video tutorial, accessible here:
https://www.youtube.com/watch?v=TzeKIbAZ80Q
Payload NextJS is an exceptional framework that combines the power of Next.js with the flexibility of a headless CMS. One of its standout features is the ability to enhance user experience through efficient data management. By leveraging features like pre-filling form values, developers can create a seamless workflow that minimizes user effort and maximizes productivity.
Imagine a scenario where users are required to fill out forms repeatedly. Every additional click or input can lead to frustration. With Payload NextJS, we can pre-fill values in forms based on existing data, allowing users to focus on what truly matters.

Pre-filling values in forms is not just a convenience; it significantly improves user experience. Here are some reasons why it matters:
To demonstrate the capabilities of Payload NextJS, we'll set up a demo project with two collections: companies and projects. Each collection will have a simple schema, with companies containing names and projects linking back to a company.
Start by creating a new Payload project and defining your collections. Ensure that the projects collection has a relationship field that connects it to the companies collection. This setup lays the groundwork for our pre-filling functionality.
In our demo project, we have defined two essential collections:
Understanding how these collections interact is crucial for implementing our pre-filling feature effectively. When creating a new project, we want the user to automatically link it to the company they are currently viewing.
To enable pre-filling, we need to create a custom UI component within the projects collection. This component will handle the logic for fetching the company ID from the URL and populating the corresponding field in the form.
First, define a new field called prefill company. This field will not add data to the backend but will serve as a frontend utility. Next, create a React component that will manage this functionality.

URL parameters are the key to our pre-filling mechanism. By appending a company ID to the URL, we can retrieve this information when users navigate to the project creation page. The format is simple: ?company=.
In your custom UI component, use the useLocation hook from React Router to access these parameters. Then, parse the query string to extract the company ID. This ID will be used to set the value of the company field in the project form.

Next, implement the logic to pre-fill the company field. Utilize the useField hook provided by Payload to manage the state of the company field. You'll want to set the value based on the parsed company ID from the URL.
Finally, make sure to handle cases where the company parameter might not exist. This ensures that your application remains robust and user-friendly.
To begin, we need to create a custom UI component that will manage the pre-filling of the company field in the project form. This component will leverage the URL parameters we discussed earlier.
Start by creating a new file named PrefillCompany.js in your components directory. This will be a React functional component that fetches the company ID from the URL and sets it in the form.

Here’s a simple structure for your component:
import React from 'react';
import { useLocation } from 'react-router-dom';
import { useField } from 'payload/components/forms';
const PrefillCompany = () => {
const { search } = useLocation();
const { setValue } = useField('company');
// Logic to parse URL and set field value
// ...
return null; // No visible UI
};This component will not render anything visible; its sole purpose is to set the value of the company field based on the URL parameter.
Now that we have our component structure, let’s implement the logic to extract the company ID from the URL.
We will use the useMemo hook to parse the URL search parameters efficiently:
import queryString from 'query-string';
const PrefillCompany = () => {
const { search } = useLocation();
const { setValue } = useField('company');
const params = queryString.parse(search);
React.useEffect(() => {
if (params.company) {
setValue(params.company);
}
}, [params.company, setValue]);
return null;
};This code checks if the company parameter exists in the URL. If it does, it sets the value of the company field using the setValue function from the useField hook.
With our prefill logic in place, it’s time to test the new functionality. Modify your button for adding a project to include the company ID in the URL.
const AddProjectButton = () => {
const { id } = useDocumentInfo(); // Import useDocumentInfo hook
const linkToCreateProject = `/projects/create?company=${id}`;
return Add Project;
};This code constructs the link dynamically, ensuring the company ID is passed as a parameter when the user clicks to add a project.
Now, when you navigate from the company view to the project creation page, the company field should be automatically populated with the correct ID. This functionality not only simplifies the user's experience but also eliminates unnecessary steps.
The implementation we just covered can drastically improve user experience in various real-world applications. Here are some scenarios where pre-filling values can be particularly beneficial:
By automating these processes, developers can create a more intuitive interface that users appreciate.
In cases where the company parameter is not present, the prefill logic we implemented ensures that the field remains empty, allowing users to select a company manually if needed.
Absolutely! You can implement similar logic for additional fields by parsing more parameters from the URL and setting their values using the same hooks.
Yes, this method is highly scalable. By using React hooks and URL parameters, you can easily adapt the pre-filling logic to fit more complex data structures and user workflows.