2024-11-22 web, development, javascript
Next.js Web App Search Functionality
By O. Wolfson
This article will guide you through setting up the project, creating the necessary components, and implementing search functionality with URL parameters.
Here is a deployed version of the blog application: https://next-search-params-cyan.vercel.app/blog
Code on GitHub: https://github.com/owolfdev/next-search-params
Prerequisites
- Basic understanding of React, Next.js, and TypeScript.
- Node.js and npm installed.
Setup the Next.js 14 Project
Create a New Next.js App:
Step 1: Setting Up Data
Create a posts.ts
file inside the data
directory with your blog posts.
Step 2: Creating the Blog Component
Create the Blog component in app/blog/page.tsx
. This component will display all blog posts and include a search bar.
Props Structure and Data Import:
- The
BlogPage
component acceptssearchParams
as a prop. This object contains URL search parameters, key-value pairs corresponding to the query parameters in the URL. - Blog post data is imported from
@/data/posts/posts.js
.
Search Functionality:
- The
searchQuery
is extracted fromsearchParams
. If thesearch
parameter exists, it is converted to a string and made lowercase. If it doesn't exist, it defaults to an empty string. - The
blogPosts
array is filtered to include only those posts whose titles contain thesearchQuery
. This filtering is case-insensitive, as both the titles and the search query are converted to lowercase.
Rendering:
- The component returns a JSX structure consisting of a div with a flex column layout.
- It renders a
SearchBar
component, which handles the updating of the search parameters in the URL. - Below the
SearchBar
, it maps overfilteredPosts
to display each post. Each post is wrapped in aLink
component to create a clickable element that navigates to the individual blog post page (/blog/${post.slug}
).
Step 3: Implementing the SearchBar Component
Create the SearchBar component in app/components/SearchBar.tsx
.
Imports and Client-Side Directive:
- Hooks
useState
,useEffect
,useSearchParams
,useRouter
, andusePathname
are imported from React andnext/navigation
.
State and Hooks:
useSearchParams
is used to access the current URL's search parameters.useState
is used to maintaininputValue
, the current state of the search input field.useRouter
provides navigation functionality, andusePathname
gives access to the current pathname of the URL.
Form Submission:
-
handleSubmit
prevents the default form submission and handles the search functionality. -
If
inputValue
is not empty, it navigates to the current pathname withinputValue
as a query parameter (?search=
). -
If
inputValue
is empty, it navigates back to the current pathname without any search parameters. -
Enhanced Navigation and URL Management:
- The
SearchBar
component demonstrates an effective way of managing URL search parameters in a Next.js 14 application. By usinguseSearchParams
for reading anduseRouter
for updating the URL, the component can dynamically control the browser's URL based on user interaction. - This approach enables a more interactive and responsive search experience. As users type and submit their queries, the URL updates immediately, reflecting the current search state. This makes the search feature more intuitive and user-friendly.
- The
Step 4: Creating the Blog Post Component
Create a component for individual blog posts in app/blog/[slug]/page.tsx
.
This function and the component together handle the rendering and static generation of individual blog post pages based on dynamic URL segments.
- Role in the Blog Application: This component is a crucial part of the blog application, enabling the functionality of viewing individual blog posts. Each post has its own unique URL based on its slug, and the content is statically generated at build time for optimal performance and SEO benefits.
generateStaticParams
Function
- Purpose: This function is used in combination with dynamic route segments (
[slug]
in this case) to statically generate routes at build time. - Implementation: It takes the
blogPosts
data (imported from@/data/posts/posts.ts
) and maps over it to return an array of objects, each containing aslug
property. These objects represent
the dynamic segments needed for each route.
- Static Generation: During the build process, Next.js calls
generateStaticParams
and uses its return value to statically generate a page for eachslug
in theblogPosts
array.
BlogPostPage
Component
- Functionality: This component is responsible for rendering the content of an individual blog post.
- Parameters: It receives
params
as a prop, which includes theslug
of the current blog post. - Data Fetching: The component uses the
slug
to find the corresponding post from theblogPosts
data. It then renders the title and content of the post. - Handling Non-Existent Posts: If a post with the given
slug
does not exist (e.g., when accessing a URL with a slug that hasn't been generated), the component will safely render nothing for the title and content, avoiding any errors.
Conclusion
This article covers setting up a blog application using Next.js 14 with TypeScript, focusing on search functionality. We've implemented a blog page with a search bar and individual blog post pages, utilizing the new App Router API in Next.js 14. The useRouter
hook is used to manage URL parameters for search functionality, demonstrating a modern approach to web application development with Next.js.