All Blog Articles

Skyrocket UX in Payload NextJS: Pre-filling Values to Enhance User Experience

Media
Sandro WegmannMarch 13, 2025
Enhance User Experience  in Payload CMS by pre-filling Values

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


Table of Contents


Introduction to Payload NextJS and User Experience

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.

Setting-Up-Collections-Payload-NextJS.png


The Importance of Pre-filling Values

Pre-filling values in forms is not just a convenience; it significantly improves user experience. Here are some reasons why it matters:

  • Time-Saving: Users can complete forms faster when fields are automatically populated.
  • Reduced Errors: Pre-filled fields minimize the chance of user errors, ensuring data accuracy.
  • User Satisfaction: A smoother process leads to happier users, increasing engagement and retention.
  • Streamlined Workflows: In complex applications, pre-filling values can simplify workflows, making it easier to manage multiple data entries.


Setting Up the Demo Project

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.


Understanding Collections: Companies and Projects

In our demo project, we have defined two essential collections:

  1. Companies: This collection includes a single field for the company name.
  2. Projects: This collection consists of a name field and a relationship field that links back to the companies.

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.


Creating a Custom UI Component for Projects

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.

Creating-Custom-UI-Component-for-Projects.png


Using URL Parameters to Pre-fill Forms

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.

Using-URL-Parameters-to-Pre-fill-Forms.png


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.


Building the Prefill Company Component

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.

Building-Prefill-Company-Component.png


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.


Implementing Logic with React Hooks

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.


Testing the New Functionality

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.


Real-World Applications and Benefits

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:

  • CRM Systems: Automatically linking contacts to companies when creating new entries saves time and reduces errors.
  • E-commerce Platforms: Pre-filling shipping information based on user profiles can streamline the checkout process.
  • Project Management Tools: Quickly associating tasks with projects or clients enhances workflow efficiency.

By automating these processes, developers can create a more intuitive interface that users appreciate.


FAQ

What if the URL parameter is missing?

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.

Can I pre-fill multiple fields?

Absolutely! You can implement similar logic for additional fields by parsing more parameters from the URL and setting their values using the same hooks.

Is this approach scalable for larger applications?

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.