javascript|May 11, 2021|1 min read

Nextjs - How to Redirect to Another Page and Pass State

TL;DR

Use Next.js Router.push() with a query object to redirect to another page and pass state, then access the passed state via router.query on the destination page.

Nextjs - How to Redirect to Another Page and Pass State

Introduction

We have a page, for example a Form. And, upon submission we would want user to be redirected to some other page and pass some state or props so that that page can show some message or so.

Next.js Router - Pass State

  • First import withRouter
import { withRouter } from 'next/router'
  • Wrap your component export with withRouter
export default withRouter(EditUser);

Now in your component’s props, you have a router object.

Redirect to other Page and Pass State

I have a user edit form, and upon submission. I want user to be redirected to /user/<id> And, I want to show a message that edit has been successful.

this.props.router.push({
  pathname: `/users/${user.id}`,
  query: {success: true}
});

Here, I’m passing a query parameter to that page. So it will be:

/users/1234?success=true

Retrieve Value in Redirected Page

In my component jsx file, I want to show a bootstrap alert.

return (
    <SimpleLayout>
      {props.router.query.success &&
        <div className="alert alert-success alert-dismissible fade show" role="alert">
          Success.
        <Button type="button" className="close" data-dismiss="alert" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </Button>
      </div> 
      }
      ...
      ...
    </SimpleLayout>
    }
)

Note the props.router.query.success

Related Posts

Nextjs - How to Build Next.js Application on Docker and Pass API Url

Nextjs - How to Build Next.js Application on Docker and Pass API Url

Introduction In this post we will see: How to prepare a docker image for your…

How to use Draft.js WYSWYG with Next.js and Strapi Backend, Edit/Update Saved Article

How to use Draft.js WYSWYG with Next.js and Strapi Backend, Edit/Update Saved Article

Introduction This post is in contuation of our previous post: How to use Draft…

How to use Draft.js WYSWYG with Next.js and Strapi Backend, Create and View Article with Image Upload

How to use Draft.js WYSWYG with Next.js and Strapi Backend, Create and View Article with Image Upload

Introduction In this post, we will use in Next.js with strapi. And, we will…

Next.js - How to Get Session Information in Server Side vs Client Side iusing Next-auth

Next.js - How to Get Session Information in Server Side vs Client Side iusing Next-auth

Introduction Next-auth is a fantastic library for abstracting handling of…

How to Integrate Next.js with Strapi Backend and Create a common utility class for REST APIs

How to Integrate Next.js with Strapi Backend and Create a common utility class for REST APIs

Introduction In this post, we will integrate Next.js with Strapi fully. And we…

How to Use Signin Signout Buttons in Next.js bootstrap project with Next-auth

How to Use Signin Signout Buttons in Next.js bootstrap project with Next-auth

Introduction In our last post, we have seen a full example of Next.js with…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…