Utilizing next-redux-wrapper within the getServerSideProps function in Next.js allows for seamless

When trying to call an action function in getServerSideProps using TypeScript, I encountered some challenges. In JavaScript, it is straightforward:

import { wrapper } from "Redux/store";
import { getVideo } from "Redux/Actions/videoAction";
//Serverside data fetching
export const getServerSideProps = wrapper.getServerSideProps(
  (store) =>
    async (context) => {
      await store.dispatch(getVideo());
    }
)

However, when attempting the same in TypeScript, issues arose.

export const getServerSideProps = wrapper.getServerSideProps(
  (store) =>
    async (context) => {
      await store.dispatch(getNews() as any);
    }
)

I used getNews() as any here, but I'm unsure if it's the correct approach.

The main challenge lies with context and async functions. This led to a specific error that I'm struggling to resolve.

This is the error I'm encountering: https://i.stack.imgur.com/yDPLr.png

Answer №1

The error is clearly explained: You must return

Promise<GetServerSidePropsResult<any>>

In your current function, you are not returning anything. To annotate getSerververSideProps:

import { GetServerSideProps } from "next";

export const getServerSideProps: GetServerSideProps =
  wrapper.getServerSideProps(async (context) => {}

If you hover over GetServerSideProps, you will see its type definition:

(alias) type GetServerSideProps<P extends { [key: string]: any; } = { 
  [key: string]: any; }, Q extends ParsedUrlQuery = ParsedUrlQuery> =
  (context: GetServerSidePropsContext<Q>) Promise<GetServerSidePropsResult<P>>

Just add a return statement:

return { props: { id: null } };

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

Guide on accessing intellisense for mapGetters, mapActions in Vuex using TypeScript without the need for class-style or decorators syntax

I have been using Vue.js and Vuex for a while now, but always with JavaScript. Recently, I decided to try using Vue with TypeScript, specifically with nuxt.js, but without utilizing decorators or style-class-components. I want to stick to the normal Vue s ...

Ways to assign values to an array within an object

I'm attempting to transfer the contents of one array into an array within an object, but I keep encountering this error: Type 'any[]' is not assignable to type '[]'. Target allows only 0 element(s) but source may have more. Here ...

How can we effectively divide NGXS state into manageable sections while still allowing them to interact seamlessly with one another?

Background of the inquiry: I am in the process of developing a web assistant for the popular party game Mafia, and my objective is to store each individual game using NGXS. The GitLab repository for this project can be found here. The game includes the f ...

Troubleshooting SPFX and Angular: Difficulty accessing service in component.ts file

I recently developed a project using the SharePoint SPFX framework and integrated all the necessary Angular (6.0 or 7.0) TypeScript packages. While Angular seems to be functioning properly within my SPFx webpart, I encountered an issue when attempting to c ...

Customizing Library component styles with CSS Modules

Having recently started working with NextJS and utilizing CSS Modules, I find myself wanting to incorporate CSS libraries like Material UI into my project. However, I'm unsure of the best way to customize the styles of components imported from these l ...

Tips for keeping the options menu visible even when the video is paused

As I was creating my own customized Video player, I encountered an issue where I wanted the options bar/menu to disappear when the user became inactive. While I have managed to achieve this functionality, I still require the option bar to remain visible ...

The seamless union of Vuestic with Typescript

Seeking advice on integrating Typescript into a Vuestic project as a newcomer to Vue and Vuestic. How can I achieve this successfully? Successfully set up a new project using Vuestic CLI with the following commands: vuestic testproj npm install & ...

Using an external HTML file to import a template into Vue.js single file components

I've been tackling a Vuejs project that involves using vue-property-decorator in single file components. I'm trying to figure out how to import the template from an external HTML (or different) file, but so far I haven't found a solution. I& ...

Set the value obtained from a resolved promise to a mutable reference object in a React component

I am in the process of developing a random movie generator. I am utilizing an external API to retrieve a list of movies and then selecting one randomly from the returned data. The current implementation is as follows: export default function Page() { con ...

Using GraphQLSchema from a different module or realm is not supported in NextJS 14

(Posting this after extensive research and finally finding a solution) I was trying to integrate type-graphql with apollo-server-lambda and nextjs (utilizing nextjs api routes for local development, while having lambdas handle my API in production). Enco ...

I'm curious as to why my t() texts are not updating in localhost/en after using i18n.changeLanguage(), but are refreshing correctly in localhost/fr

Hello There I recently created a website with dark mode and multi-language support for testing purposes, but I encountered an issue along the way. The Problematic Code I have removed any unnecessary elements from the code snippet below: portfolio/src/pag ...

Limit the covariance of properties in generic arguments

When I define a generic argument of object type with specific mandatory properties (for example, in this function it requires the object to have timestamp: string), TypeScript allows for using a more specialized attribute type as a generic argument - as sh ...

What is a more precise way to define an Object type on a temporary basis?

I'm currently working with Angular 2. Typically, when I specify a type, I start by creating an interface: interface Product { name: string, count: number } and then use it like this: let product: Product; However, now I need to temporarily de ...

Guide on retrieving POST data in sveltekit

Hello, I'm new to sveltekit and I am trying to fetch post data in sveltekit using a POST request. Currently, I am utilizing axios to send the post data. const request = await axios .post("/api/user", { username, email, ...

The Angular @Input directive may be prone to receiving inaccurate model data

I am currently working on setting up @Input for my component using a model that resembles the following: interface Car { sail?: never tires: number weight: number } interface Boat { tires?: never sail: boolean weight: number } exp ...

Configuring environment variables in Next.js

I am currently in the process of configuring the ENV in Next.js Within next.config.js file const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin"); const withSass = require('@zeit/next-sass'); module.exports = withSass({ target: ...

The onSubmit function in Formik fails to execute if there are no input values present

I am currently working on building a form using Next.js, TypeScript, and the Formik + Yup libraries. I've encountered two scenarios: one where an input field is visible and Formik captures the value, and another where the input is not visible and the ...

Discover the steps to extend static generic methods in Typescript

My issue lies in compiling Typescript code as the compiler doesn't seem to recognize the inheritance between my classes. Whenever I attempt to compile, an error arises: Property 'create' does not exist on type 'new () => T'. ...

Guide on incorporating external .d.ts files for a module

I'm currently delving into the process of utilizing an external .d.ts file that is not included in the module. My intention is to make use of xlsx, which lacks its own type definitions, and instead incorporate them from the package @types/xlsx. Afte ...

Incorporating a picture backdrop into a button element in a React Typescript component

I am working on a React project with TypeScript and using a Material UI library. I am trying to set a background image for a button, but when I use src or imageURL, it gives me a TypeScript error. The CSS style also does not show the picture. Here is my ...