2

I have the code below. It redirects to /home and passes a username variable. However, in the address bar, it shows http://localhost:3000/home?username=bobmarley. How can I pass variables using next.js cleanly?

import { withRouter } from 'next/router'
loginUser(this.state.user)
        .then(response => {
          var username = response['username'];

          if (username) {
            this.props.router.push({
              pathname: '/home',
              query: { username: username }
            });
          }
      });

2 Answers 2

1

Use the as prop of router.push, this will be what is displayed in the address bar. Set it to the same value used for pathname.

as - Optional decorator for the URL that will be shown in the browser.

this.props.router.push({
  pathname: '/home',
  as: '/home',
  query: { username: username }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Hey I did this and I still got the username in my url bar?
0

This is not where you put the as parameter. It should look like

this.props.router.push({
  pathname: '/home',
  query: { username: username }
},
{
pathname: '/masked-url'
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.