javascript - 在 typescript 中使用 react-redux connect

标签 javascript reactjs typescript redux react-redux

我尝试使用 redux 和 react-router-dom 在 typescript 中构建一个 React 应用程序。当我将 redux 添加到我的应用程序时,我遇到了输入问题。因此,我创建了以下只有一页的最小示例 test-page:

App.jsx

import * as React from 'react';
import { Route, Redirect } from 'react-router-dom'
import Test from './containers/test-page'
import './App.css';

class App extends React.Component {
  render() {
    return (
      <div className="ui container" id="main">
        <Route exact path="/" render={() => <Redirect to="/test" />}/>
        <Route exact path="/test" component={Test} />
      </div>
    );
  }
}

export default App;

测试页面的容器如下所示。它会在调用 connect 时产生输入错误。

containers/test-page/index.tsx

import { Dispatch } from 'redux'
import { connect } from 'react-redux'
import TestPage from './test-page'

function mapDispatchToProps(dispatch: Dispatch<any>) {
  return dispatch({ type: 'ALERT_USER' });
}

function mapStateToProps(state: any) {
  return { label: 'my test label' }
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(TestPage)

容器使用以下 react 组件,在生产中应该为路由器呈现页面。它产生两个错误,见下文。

containers/test-page/test-page.tsx

import * as React from 'react';

export namespace Test {
  export interface Props {
    alert: () => void;
    label: string;
  }

  export interface State {
  }
}

export default class TestPage extends React.Component {

  constructor(props?: Test.Props, state?: Test.State, context?: any) {
    super(props, context);
  }

  sendAlert = () => {
      this.props.alert()
  }

  render() {
    return (
      <div>
        <h1>Test</h1>
        <button onClick={this.sendAlert}>{this.props.label}</button>
      </div>
    );
  }
}

错误信息:

proxyConsole.js:54 ./src/containers/test-page/test-page.tsx
(20,18): error TS2339: Property 'alert' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>'.

proxyConsole.js:54 ./src/containers/test-page/test-page.tsx
(27,54): error TS2339: Property 'label' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>'.

proxyConsole.js:54 ./src/containers/test-page/index.tsx
(16,3): error TS2345: Argument of type 'typeof TestPage' is not assignable to parameter of type 'ComponentType<{ label: string; } & { type: string; }>'.
  Type 'typeof TestPage' is not assignable to type 'StatelessComponent<{ label: string; } & { type: string; }>'.
    Type 'typeof TestPage' provides no match for the signature '(props: { label: string; } & { type: string; } & { children?: ReactNode; }, context?: any): ReactElement<any> | null'.

我尝试遵循不同的指南并查找示例实现,但无法解决这些问题。我不明白 typescript 编译器的错误信息:

  • 为什么我定义的属性在 this.props 上不存在?
  • connect 中到底有什么是不可分配的?

最佳答案

我注意到几件事:

1) 据我在示例中看到的以及在 TypeScript 中使用 props 时,您对 React.Component 的调用需要指定 Props 作为类型参数,如下所示:

export default class TestPage extends React.Component<Test.Props, Test.State>{

    constructor(props: Test.Props) {
        super(props);
    }

}

您可以通过传递空接口(interface)来指定您的组件不接受propsstate,即:

export default class TestPage extends React.Component<{}, {}>{

    // constructor becomes 'useless' at this point and is not needed
    constructor() {
        super();
    }

}

我认为这解释了为什么您的调用签名不匹配以及为什么 this.props 上没有可见的属性 - TS 看到了 ReadOnly{} 的接口(interface),因为它没有传入类型参数。

2) 您的 mapStateToProps 函数看起来不太正确。 mapStateToProps 有两个参数,state(引用你的 Redux store)和 ownProps 作为可选的第二个参数,它指的是到 props 从父级传递下来。所以 mapStateToProps 应该是这样的:

function mapStateToProps(state: any, ownProps: { label: string }) {

    return {
        label: ownProps.label
    };
}

这是我对 connect 抛出错误的原因的猜测 - 它只是一个地方,您可以在这里断言 Redux 应该如何处理来自 的组合 props >storeprops 来自父级。让我知道这是否可行。

关于javascript - 在 typescript 中使用 react-redux connect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47104264/

相关文章:

javascript - 使用我自己的模型类型属性模拟 Angular2/typescript 的数据

javascript - 使用点击功能 react JS 渲染元素并将索引传递给点击功能

javascript - VueJS 获取与路由匹配的信息

reactjs - React Router v4 - 如何获取当前路由?

unit-testing - 测试具有服务依赖性的服务

reactjs - 需要一个参数取决于另一个参数的存在

javascript - 如何获取innerHTML的css类?

javascript - Vue 数组的点击事件

javascript - 当父状态改变时强制子组件更新

javascript - react native : Disabling scene transitions in NavigationExperimental