Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

275
Views
Next.JS How to pass parameters to a callback function

so this is my code:

import Router from "next/router";
import React from "react";

export default function MainIndex() {
    return (<React.Fragment>
        <h1>Main Index Page</h1>
        <button onClick={() => Router.push('/family')}>Family</button>
        <button onClick={() => Router.push('/dynamic-id-page')}>Dynamic-id-page</button><br /><br />
        <button onClick={() => Router.push('/fav-games')}>World Of WarCraft</button>

    </React.Fragment>);
}

My question is: How can I create a function so I can use it as a callback for different routes.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should use "useRouter" :

import {useRouter} from "next/router"

then

export default function MainIndex() {
  const router = useRouter();
  const navigate = (event) => (url) => router.push(url)
...}

Then

<button onClick={navigate("/family")}> Family </button>

But keep in mind that this kind of "optimization" isn't really one... The inline function in your original code is easier to understand and premature optimization is the root of all evil

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!