Is there a way to refresh the page in NextJs whenever the parameters are modified without the need for reloading?

Currently, I am navigating on

http://localhost:3000/?groupId=chicago&dayOfWeek=tuesday
. Despite pressing a button on a component, the desired outcome of transitioning to another day, like Thursday, is not occurring.

import { useRouter, usePathname, useSearchParams } from "next/navigation";

const SelectDayDialog = ({ groupId, organization }) => {
    const router = useRouter();
    const thursday = "Thursday"; 

    const handleGroupSelection = (dayOfWeek) => {
        const query = { groupId: groupId, dayOfWeek: dayOfWeek.toLowerCase() }; 
        const queryString = new URLSearchParams(query).toString();

        let routeUrl = `/?${queryString}`;

        router.push(routeUrl);
    };

    return (
        <div>
            <button onClick={() => handleGroupSelection(thursday)}>Thursday</button>
        </div>
    );
};

Upon clicking the button, the url transitions to

http://localhost:3000/?groupId=chicago&dayOfWeek=thursday
. However, there is no update in the page content or components.

How can I resolve this issue?

Answer №1

// server.tsx (page component)
interface PropsTableScompra {
    searchParams: {
        [type: string]: string | undefined;
    };
}

async function MovementsPage({ searchParams }: PropsTableScompra) {

   
    const groupId = searchParams?.groupId || '';
    const dayOfWeek = searchParams?.dayOfWeek || '';

    // frontend component
    <SelectDayDialog  groupId={groupId} dayOfWeek={dayOfWeek} />
......

for the frontend component, utilize router.replace

https://i.sstatic.net/65SriwCB.png

make sure to include "use client" in the top of the client component code as it utilizes the onChange event ....

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Vuejs single-file component fails to display on the page

Even though there are no errors in the browser and Webpack compiles successfully, the message "hello from dashboard" is not displaying on the page. I am currently using Vue version 2.6 In my main.js file: import Vue from 'vue' Vue.component(&a ...

What is the best way to start up node.js and react?

Can you help me with running a server node js and a react app simultaneously? Here is my package.json { "name": "mon-app", "version": "0.1.0", "private": true, "dependencies": { "express": "^4.17.1", "react": "^16.14.0", "react-dom": ...

Both if and else statements are carrying out code in JavaScript/jQuery

The second if statement is functioning correctly, but the first one always triggers the else statement and never stands alone. This jQuery function is enclosed within another function that is invoked under the "$(document).ready" command. I resorted to u ...

Issues related to ng-model within a dropdown list

Currently, I am facing an issue with retrieving the selected value from a select element using ng-model. Even though the value is displayed correctly on the application page, it remains at the initial value in the app controller. Despite my efforts to find ...

Material-UI style is taking precedence over custom React Component styles

RELATED QUESTION ON THIS LINK: Styles being overwritten by Material-UI style I am currently developing a component library based on Material UI. I want to pass styles to my custom components using JSS. However, I am facing challenges with the higher speci ...

Learn how to call JavaScript code from an HTML file using Ajax

I am encountering an issue with my website. The index.html page contains JavaScript code that triggers an AJAX request to show the content of other.html inside a div in index.html. However, despite the other.html loading correctly, the JavaScript code wit ...

Buttons within the Bootstrap carousel caption cannot be clicked

I am currently designing a webpage with a Bootstrap carousel that includes a paragraph caption and two buttons. However, the buttons are not functioning as clickable links - when clicked, nothing happens. {% block body %} <div id ="car-container"> ...

Obtaining the ID from a JSON object in react.js ES6: A Comprehensive Guide

I have an array of JSON objects containing properties like Name, ID, and Address. My goal is to retrieve the IDs from all objects in this JSON array using react.js ES6. If anyone could offer guidance on how to accomplish this task, it would be greatly appr ...

Transforming a string in AngularJS to a "controller as" approach using $parse

How can I modify foo.callbacke to reference the timerController.callbacke method? <div ng-app="timerApp" ng-controller="timerController as foo"> <div ng-repeat="item in [1,2,3,4]"> <div watcher="{'seconds': 'foo.callbacke ...

Identifying Mistakes to Address Promise Failures

I encountered unhandled promise rejection errors and attempted to catch them and log them after each .then statement. Unfortunately, I ran into an 'unexpected token' issue when running the code on my server due to the period before .try. Despit ...

Ways to efficiently implement configuration settings in a functional approach

I've delved into the world of functional programming and I'm working on developing a package that needs to be configurable. My goal is to set up this configuration only once to maintain purity in my functions without relying on global state. Curr ...

How to avoid line breaking in Select component with icon and text using React Material-UI

I am working on a Select element that includes both ListItemIcon and ListItemText. Whenever the select option is chosen, there seems to be an unwanted line break appearing. Despite finding various workarounds for this issue, I am keen on discovering the ...

Displaying text from an array in a div using JavaScript with a 2-second delay

I am relatively new to this, so please forgive me if something is not quite right. I have an array filled with different strings of text, and I want to display each string in an HTML div with a 2-second delay between each line. While I have managed to show ...

What is the best way to show the totals on a calculator screen?

As part of my work, I created a calculator to help potential clients determine their potential savings. Everything seems to be working fine, except for the total fees not appearing for all the boxes. I believe I might be missing the correct process to add ...

discord.js-commando encountered an error: unable to find the fetchUser function

After successfully coding a discord bot on my personal computer using nodejs and discord.js-commando, I decided to transfer it to my Raspberry Pi 3b. I installed the latest version of nodejs, used scp to transfer the files, but encountered an error when tr ...

Transforming a redux form onSubmit function into a promise-based structure

One of my goals is to promisify the onSubmit handling in my submitForm for redux form. You can find a similar example here. submitForm = () => { return this.props.submituserForm() .then(() => { console.log('test') }) ...

Error message: Impossibile to locate module 'formik' in 'C:Live projectsNew folder (2)src\_metronicpartialsmodalscreate-app'

"dependencies": { "@formatjs/intl-pluralrules": "^4.0.28", "@formatjs/intl-relativetimeformat": "^9.1.7", "@fortawesome/fontawesome-free": "^5.15.3", "@popperjs/core": "~2.10.1", "animate.css": "^4.1.1", "apexcharts": "^3.27.1", ...

Is the concept of client fanout truly beneficial?

I'm in the process of constructing a community platform on firebase that features timelines and posts similar to those found on Facebook. When a user creates a post, it should automatically appear on the timelines of their followers. Google proposes ...

Leveraging the Railway Pathway from the Google Maps API

I need to customize my map to display only railway stations instead of the entire map. How can I achieve this? Below is the code I have attempted: <html> <head> <style type="text/css"> html { height: 100% } ...

Essential Understanding of HTML Query Strings Required

As a newcomer to the world of web design, I have taken on what seems like a challenging task for me: creating a webpage that can send a query string to the German Railway website (bahn.de) with the parameters I input. My question now is whether there is a ...