In Next.js, the switch button remains in the same state even after the page is refreshed

Is there a solution for this issue? I am currently developing a switch button for a configuration page. The problem arises when I toggle the switch from active to maintenance mode, save it successfully, but upon refreshing the page, the switch reverts back to active status.

[Before refresh][1] [1]: https://i.sstatic.net/eDYQp.png

If you observe the image before refreshing and after refreshing the page, you will notice that the status changes back to active. How can I ensure that the changed status is retained even after page refresh in next js?

Here's the updated code snippet:

import React, { useEffect, useState } from 'react'
import { useRouter } from 'next/router'
import { WebConfigurationType, WebConfigType } from '@/type/web'
import { getData, postData, putData } from '@/utility/fetch'
import { showMessage } from '@/components/alerts/showMessage';
...

I have associated this with a JSON schema as follows:

{ "id": "d9cbf36e-bb51-11ee-a740-00155db75f50", "name": "Beranda", "code": "home", "position": 1, "is_maintenance": false }

Answer №1

Begin your code with the following:

import React, { useEffect, useState } from 'react'
import { useRouter } from 'next/router'
import { WebConfigurationType, WebConfigType } from '@/type/web'
import { getData, postData, putData } from '@/utility/fetch'
import { showMessage } from '@/components/alerts/showMessage';

Replace it with this snippet:

const React = require('react');
const { useEffect, useState } = require('react');
const { useRouter } = require('next/router');
const { WebConfigurationType, WebConfigType } = require('@/type/web');
const { getData, postData, putData } = require('@/utility/fetch');
const { showMessage } = require('@/components/alerts/showMessage');

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 presence of catchError() within the pipe() function will display an error specifically at the .subscribe stage

I encountered an issue while trying to handle errors for a method in Angular. After adding a catchError check using the .pipe() method, I noticed that the variable roomId was marked with a red squiggly line. The error message read: TS2345: Argument of type ...

React.js reusable component fails to update when a dependency library used in useEffect() undergoes a change

One of the components I've created in Next.js is a custom routing link that appends the locale before the pathname when clicked and applies a custom style for the active link. Below is the code snippet for this component: "use client"; imp ...

Encountering issues while deploying Heroku with React and Next.js

After successfully running my react/next js app with no errors or warnings on my localhost, I encountered deployment issues on Heroku. Each time I try to deploy, I consistently receive an error indicating that the posts array (in pages/landing.js) is undef ...

Only map property type when the type is a union type

My goal is to create a mapping between the keys of an object type and another object, following these rules: Each key should be from the original type If the value's type is a union type, it should have { enum: Array } If the value's type is not ...

The performance of the Next.js <Image /> component is sluggish on Netlify but significantly faster on Vercel

I have a fairly simple Next.js application and I am utilizing the <Image /> component in Next.js to display two small images like this: <Row className="justify-content-center m-0 mt-3 text-center"> <Col xs={4} md={3} lg={2} class ...

Transmitting data from Angular to .NET Core for seamless integration

I have been attempting to send an xls or any other file from my angular application to a .NET core controller, but none of my methods seem to work... Below is my component where I call my service upon button click: handleFileInput(file: FileList) { this. ...

TypeScript Definitions for Material-UI version 15

Is there a Material-UI v15 TypeScript definition file in the works? I need it for a project I'm working on and as a TypeScript newbie, I want to make sure the custom file I've begun is accurate. ...

Unlocking the Power of CheerioJS for Selecting Table Elements

I am struggling to figure out how to use CheerioJS to parse HTML table values and convert them into a JSON object. Despite my efforts, I have only been able to come up with a convoluted solution that doesn't quite work. My goal is to extract informa ...

What's the best way to replicate a specific effect across multiple fields using just a single eye button?

Hey everyone, I've been experimenting with creating an eye button effect. I was able to implement one with the following code: const [password, setPassword] = useState('') const [show, setShow] = useState(false) <RecoveryGroup> ...

Error Encountered: Unhandled Runtime Error in Next.js with Firebase - TypeError: Unable to access the property 'initializeApp' as it is undefined

It's baffling why this error keeps appearing... my suspicion is directed towards this particular file. Specifically, firebaseAuth={getAuth(app)} might be the culprit. Preceding that, const app = initializeApp(firebaseConfig); is declared in "../f ...

Is it possible to create an instance in TypeScript without using class decorators?

As per the definition on Wikipedia, the decorator pattern enables you to enhance an object of a class with additional functionalities, such as: let ball = new BouncyBall(new Ball()) The Ball object is adorned with extra features from the BouncyBall class ...

Using TypeScript to import npm modules that are scoped but do not have the scope name included

We currently have private NPM packages that are stored in npmjs' private repository. Let's say scope name : @scope private package name: private-package When we install this specific NPM package using npm install @scope/private-package It ge ...

Exploring Ways to Access Console.log() from Client Pages in Next.js

Struggling to debug my client-side renderings using `Console.log()` (Any tips on an easier way to use the debug tool would be appreciated), as I can only access server-rendered console logs. I'm attempting to track the flow of information for this ha ...

typescript throwing an unexpected import/export token error

I'm currently exploring TypeScript for the first time and I find myself puzzled by the import/export mechanisms that differ from what I'm used to with ES6. Here is an interface I'm attempting to export in a file named transformedRowInterfac ...

Having trouble accessing a prop from the Link Component address in Next.js?

I am currently working on a project where I have a link component that is structured like this: '<Link href={`/product/${id.id}`}>' After clicking the link, the URL that appears in the address bar is as follows: http://localhost:3000/produ ...

When trying to use the exported Ionic 3 class in TypeScript, the error "Cannot find name 'ClassName'" occurs

When working on my Ionic 3 Application, I encountered an error stating "Cannot find name" when trying to use certain plugins. I initially imported the plugin like this: import { AndroidPermissions } from '@ionic-native/android-permissions'; and ...

utilizing $inject method along with supplementary constructor parameters

After referencing the answer found here: Upon implementing the $inject syntax, my controller code appears as follows: class MyCtrl { public static $inject: string[] = ['$scope']; constructor($scope){ // implementation } } // register ...

Unit Testing Angular: Passing FormGroupDirective into a Function

I am currently writing unit tests for a function that takes a parameter of type FormGroupDirective. I have been able to test most of the logic, but I'm unsure about what to pass as a parameter when calling the resetForm() function. Here is the code sn ...

Is it possible to dynamically close the parent modal based on input from the child component?

As I follow a tutorial, I am working on importing the stripe function from two js files. The goal is to display my stripe payment in a modal. However, I am unsure how to close the modal once I receive a successful payment message in the child. Below are s ...

Why isn't the constraint satisfied by this recursive map type in Typescript?

type CustomRecursiveMap< X extends Record<string, unknown>, Y extends Record<string, unknown> > = { [M in keyof X]: M extends keyof Y ? X[M] extends Record<string, unknown> ? Y[M] extends Record<st ...