简体   繁体   English

Next.Js Redux Wrapper w/Typescript - 实现错误,存储不存在为类型

[英]Next.Js Redux Wrapper w/ Typescript - Implementation error, store does not exist as type

I am implementing state management for React.我正在为 React 实现状态管理。 I am using Next.js and Typescript.我正在使用 Next.js 和 Typescript。

I used the next redux wrapper and have the following on /pages/_app.js as instructed by the guide.我使用了下一个 redux 包装器,并按照指南的指示在 /pages/_app.js 上有以下内容。

import React from "react";
import {createStore} from "redux";
import {Provider} from "react-redux";
import App, {Container} from "next/app";
import withRedux from "next-redux-wrapper";
import configureStore from '../state/store';

class MyApp extends App {

    static async getInitialProps ({Component, ctx}) {
        return {
          pageProps: (Component.getInitialProps ? await Component.getInitialProps(ctx) : {})
        }
      }

    render() {
        const {Component, pageProps, store} = this.props;
        return (
            <Container>
                <Provider store={store}>
                    <Component {...pageProps} />
                </Provider>
            </Container>
        );
    }

}

export default withRedux(configureStore)(MyApp);

I get a typescript error on the store variable I'm trying to declare in the render() function, which throws me an error as follows:我试图在 render() 函数中声明的 store 变量上出现打字稿错误,这会引发如下错误:

const store: any
Property 'store' does not exist on type 'Readonly<AppInitialProps & { Component: NextComponentType<NextPageContext, any, {}>; router: Router; }> & Readonly<{ children?: ReactNode; }>'.ts(2339)

I am unsure as to what the issue is.我不确定问题是什么。 I am not able to access any type/interface to allow store to be a variable.我无法访问任何类型/接口以允许 store 成为变量。 And this is interesting because this is the solution for many developers when using Typescript/Redux/Next.js.这很有趣,因为这是许多开发人员在使用 Typescript/Redux/Next.js 时的解决方案。

Any help/pointers would be appreciated, thank you.任何帮助/指针将不胜感激,谢谢。

Container is deprecated now; Container现已弃用; use App instead and pass it props with Store .改用App并通过Store传递道具。 Setting something to 'any' because it's giving you errors is not a good solution and kind of defeats the whole purpose of using TypeScript in the first place.将某些东西设置为“任何”,因为它会给您带来错误,这不是一个好的解决方案,并且首先违背了使用 TypeScript 的全部目的。

import App, { AppContext } from 'next/app'
import { Provider } from 'react-redux'
import withRedux from 'next-redux-wrapper'
import { Store } from 'redux'
import Page from '../components/Page'
import { initializeStore } from '../store'

interface Props {
  store: Store;
}

class MyApp extends App<Props> {
  static async getInitialProps({ Component, ctx }: AppContext) {
    return {
      pageProps: (Component.getInitialProps 
        ? await Component.getInitialProps(ctx) 
        : {}),
    }
  }

  render() {
    const { Component, pageProps, store } = this.props

    return (
      <Provider store={store}>
        <Page>
          <Component {...pageProps} />
        </Page>
      </Provider>
    )
  }
}

export default withRedux(initializeStore)(MyApp)

Fixed the issue.修复了问题。

I added a simple我添加了一个简单的

interface Props {
    store: any
}

To circumvent the Typescript error.规避打字稿错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Next.js:使用带有 TypeScript 的 next-redux-wrapper 在 getServerSideProps 中调用 Thunks? - Next.js: Calling Thunks in getServerSideProps with next-redux-wrapper with TypeScript? 为什么这个 next.js 和 typescript 页面显示“类型上不存在属性‘数据’” - Why this next.js and typescript page says "Property 'data' does not exist on type" Next.js GetServerSideProps 自定义包装器 TypeScript - Next.js GetServerSideProps custom wrapper TypeScript Next.js 项目 + TypeScript 中存储的最佳类型 - The best proper type for store in Next.js project + TypeScript Redux 工具包 typescript 类型问题与 next js 和 next-redux-wrapper - Redux toolkit typescript type issue with next js and next-redux-wrapper 使用 Next.js 摆脱 redux 包装器中的控制台日志 - Getting rid of console logs in redux wrapper with Next.js Express.js API w/ Next.js:csurf 实现 - Express.js API w/ Next.js: csurf implementation 在Typescript Reac + Redux中具有联合类型的类型错误中,属性不存在 - Property does not exist on type error with Union Types in Typescript Reac + Redux Next.js和redux。 在服务器端填充存储不会生效 - Next.js and redux. Populating store on server side does not take effect Next.js 与 typescript - useContext 错误 - Next.js with typescript - useContext error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM