Maximizing Redux DevTools integration with Redux Toolkit and Next.js for TypeScript projects

The initial state is visible in the DevTools, but any actions taken after the code has rendered do not show up.

In pages/_app.tsx, I have implemented the following:

import getStore from '../store/store'

export default function MyApp({ Component, pageProps }: AppProps) { 
  const store = getStore(pageProps.initialState);
  return (
    <Provider store={store}>
      <Component {...pageProps} />
    </Provider>
  );
}

If it wasn't for the above setup (where props need to be passed before initializing the state), @Firmino Changani would be correct. However, I can't run getStore at the store because I wouldn't obtain the initial state.

Here's the content of the store:

import { configureStore, ThunkAction, Action, combineReducers } from "@reduxjs/toolkit";
import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux";
import UISlice from '@todoSlice/todoSlice'
const rootReducer = combineReducers({
  todo: UISlice,
});
export default function getStore(incomingPreloadState?: AppState) {
  const store = configureStore({
    reducer: rootReducer,
    preloadedState: incomingPreloadState,
  });
  return store;
}
export type AppState = ReturnType<typeof rootReducer>;
export type AppDispatch = ReturnType<typeof getStore>["dispatch"];
export type AppThunk<ReturnType = void> = ThunkAction<
  ReturnType,
  AppState,
  unknown,
  Action<string>
>; 
export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<AppState> = useSelector;

This is the code for the page itself:

import type { NextPage } from 'next' 
import getStore, { useAppDispatch, useAppSelector, AppState }  from '@store/store'
import   { intidialTodos } from '@todoSlice/todoSlice'


export async function getServerSideProps() {
  const store = getStore();
  await store.dispatch(intidialTodos());
  return {
    props: {
      initialState: store.getState(),
    },
  };
}
const Home: NextPage = () => {
  const dispatch = useAppDispatch();
  const categories = useAppSelector( ( state: AppState ) => state.todo.categories );
  const addTodo = () => dispatch(addTodos({name: "The one", id: 506}))
  return (
    <><button onClick={addTodo}>Add!</button> 
   .....
  )}

I believe we shouldn't expect the intidialTodos action triggered from getServerSideProps to appear in the actions panel of DevTools. But when I click the add button, I should see the action in DevTools and observe the newly added item in the state, correct?

The application functions correctly, the new item gets added as expected, but nothing beyond @@INIT shows up in the Redux DevTools.

I attempted the following approach, but it did not yield results:

import { composeWithDevTools } from 'redux-devtools-extension';
import UISlice from '@todoSlice/todoSlice'
import {createAsyncThunk} from '@todoSlice/todoSlice'; 
const rootReducer = combineReducers({
  todo: UISlice,
});
export default function getStore(incomingPreloadState?: AppState) {
  const composeEnhancers = composeWithDevTools({ actionCreators: [createAsyncThunk], trace: true, traceLimit: 25 });
  const store = configureStore({
    reducer: rootReducer,
    preloadedState: incomingPreloadState,
    devTools: false,
    enhancers: [composeEnhancers({ realtime: true, port: 8000 })],

  });
  return store;
}

Answer №1

Your devTools are currently set to false. Below is the setup I have for a Next.js app:

import { createLogger } from "redux-logger";
const logger = createLogger();

export function initializeStore() {
  return configureStore({
    devTools: true,
    middleware: [logger],
    reducer: {/* My reducers */},
  });
}

const store = initializeStore();

Answer №2

Save yourself some time and use the next-redux-wrapper package instead of trying to recreate it on your own. It covers everything you're attempting to do here.

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

Tips for restricting the voting feature on a Facebook App to only once every 24 hours

Recently, I've been delving into the world of back-end development and decided to create a Facebook app that allows multiple photo entries with voting capabilities. Users can vote on one or multiple entries each day, and the system automatically captu ...

Retrieving a JSON object using a for loop

I'm working on a basic link redirector project. Currently, I have set up an Express server in the following way: const express = require('express'); const app = express() const path = require('path'); const json = require('a ...

The curious case of looping and peculiar Vue actions

I've been working on a project where I am looking to dynamically add more input fields upon clicking a button. My initial attempt using jQuery.append ran into issues with detecting v-model. Switching gears, I decided to try achieving the same outcom ...

Tips for avoiding Client DOM XSS vulnerability in JavaScript

Upon completing a checkmarx scan on my code, I received the following message: The method executed at line 23 of ...\action\searchFun.js collects user input for a form element. This input then passes through the code without proper sanitization ...

Struggling to access the height attribute from a CSS file

Hey there. I'm pretty confident that the solution to my query is quite simple. So, I have a .css file with this particular code snippet: div.site { text-align:center; border:2px solid #4b6c9e; padding:1px; margin-top:10px; font-size:medi ...

Is there a way to use JavaScript to modify the position of a div element

Can we adjust the div position using CSS (absolute or relative) with JavaScript? Here's an example code snippet: <div id="podpis" style="margin-top: 2rem;"> <div class="invoice-signature"> <span><?=$xml->sanitiz ...

Why doesn't the let variable used in a for loop initialization extend its scope to the enclosing block?

I've always been puzzled by this question: If block scopes are generated when a let or const identifier is enclosed within curly brackets, then how come the let identifier in the initialization statement of a for loop isn't accessible in the oute ...

Guide to sending an ajax request from one page and receiving the response on a different page

Looking for advice on sending Ajax requests using jQuery and HTML5. I have multiple pages within my application. Is it possible to send an Ajax request from one page (e.g sync.html) and receive a response on another page (e.g home.html)? I am aware of alte ...

Struggling with uploading files in AngularJS?

Below is the code snippet from my controller file where I aim to retrieve the values of various input elements (name, price, date, image) and store them in an array of objects... $scope.addBook = function(name, price, date, image) { name = $scope ...

Determining the height of a Bootstrap column in relation to another element

Utilizing the grid layout from bootstrap, I have the following structure: <div id="prof_cont_enclose"> <div class="row"> <div class="prof_cont_row"> <div class="col-xs-12 col-sm-4 col-md-2 col-lg-2 prof_elem"&g ...

Is it possible to use the `new Image()` method in Next.js

Is it possible to use the new Image() function within Next.js? I am encountering an error that states it is not defined. I am aware that I can easily use <Image> inside JSX, but in this particular situation, I am unsure if it will work because I nee ...

Enhanced data visualization with Material UI's nested datagrid feature

Is there a way to display nested JSON data on a React Material UI data grid? I'm looking to showcase the phone numbers of users from the JSON in the provided sandbox example. You can check out the demo here. ...

Moving the Anchor of Material UI's MultiSelect Popup on Selection

After updating from Material UI 4.2.0 to 4.9.10, I observed a change in behavior that seems to have originated in version 4.8.3. When using a Select element with the multiple attribute, I noticed that the popup menu shifts after selecting the first item. ...

Tips on getting the Jquery .load() function to trigger just once and executing an Ajax request only once

Upon loading the page, I am utilizing the JQuery .load() function to retrieve content from a PHP file. The content loads successfully but it keeps reloading continuously as observed through Chrome Developer tools. I only want the content to load once. var ...

What is the best way to enhance an object using a class in ES6?

In an effort to improve the clarity of my ES6 class definition, my current code looks like this: class SomeClass { constructor({a, b, c, d, e}) { this.a = a; this.b = b; this.c = c; this.d = d; this.e = e; // additional code here ...

Refresh the current page with jQuery Mobile when it is clicked

I have a multi page template in jQuery Mobile. How can I refresh the current page when clicking on a hyperlink or button? I am using JQM version 1.4.5 Despite trying the code suggested in how to refresh(reload) page when click button in jQuery Mobile, it ...

Creating unique border-radius for each point in a Highcharts column chart with React

Let's flip the script and start at the finish line. My goal is to customize my column chart to resemble this design: https://i.stack.imgur.com/FckJB.png Creating this style is a breeze with chart.js Credit: I've already delved into this inquiry ...

Tips for restricting the size of uploaded files or the quantity of files in multer express and effectively managing any errors

I am currently working on a code that requires me to set limits on file size or the number of files that can be uploaded. For instance, I want to restrict users from uploading more than 100 files at once or limit the total upload size to 100 mb. However, ...

Exploring the plane intersection within a 3D object using Three.js

I attempted to create an animation using Three.js to display the intersection plane on a 3D object with the following code snippet: import React, { useRef, useEffect, useState } from 'react'; import * as THREE from 'three'; export cons ...

What is the best way to insert a record into the rth column of the nth row in a table

In the table I'm working with, there are 6 columns and only 5 of them have data filled in. The last column is currently empty for all rows. I am now trying to populate the last column of each row with some data. Can someone guide me on how to use a f ...