Redux与Typescript错误连接

时间:2018-04-01 19:20:22

标签: reactjs typescript redux

我将redux实施到一个新项目中,在我使用connect()(App)连接应用程序之前,一切都进展顺利。我收到以下的Typescript错误,似乎无法确定如何正确输入。

(13,16): Property 'dispatch' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>'.

以下是我的应用代码。

&#13;
&#13;
import * as React from 'react';
import { connect } from 'react-redux';
import { handleInitialData } from '../../actions/shared';


import './App.css';

const logo = require('./logo.svg');

class App extends React.Component {
  
  componentDidMount() {
    this.props.dispatch(handleInitialData());
  }

  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Welcome to React</h1>
        </header>
        <p className="App-intro">
          To get started, edit <code>src/App.tsx</code> and save to reload.
        </p>
      </div>
    );
  }
}

export default connect()(App);
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

从代码中我发现的是

this.props.dispatch(handleInitialData()); // this wont work

function mapDispatchToProps(dispatch){
    return bindActionCreators({  // please import bindActionCreators from redux
       // here you write your actions
         dispatch: handleInitialData 
    },dispatch)
}



function mapStateToProps(state){
    return {
               //redux state
    }
}



export default connect(mapStateToProps, mapDispatchToProps)(App);