2016-07-15 36 views
3

我想知道爲什麼點擊錶行仍然會給我undefined調度錯誤,導致Uncaught TypeError: dispatch is not a function。我一直在關注這個post來嘗試解決這個問題,但迄今爲止沒有成功。調度沒有在redux中定義與reactjs

ListingTable.js

import Table from 'grommet/components/Table'; 
import React from 'react'; 
import {remove, add} from '../action/ListAction'; 
import {connect} from 'react-redux' 

let createHandlers = function(dispatch) { 
    let onClick = function(item) { 
     dispatch(add(item)) <<== undefined!!!! 
    }; 
    return { 
     onClick 
    }; 
}; 
export class ListingTable extends React.Component { 

    constructor(props) { 
     super(props); 
     this.handlers = createHandlers(this.props.dispatch); 
    } 
    render() { 
     return <Table> 
       <tbody> 
       { 
        this.props.data.map((item, key)=> 
         (
          // Undefined Error!!!! 
          <tr onClick={()=> this.handlers.onClick(item)} key={key}> 

           <td>{item.user_name}</td> 
           <td>{item.name}</td> 
           <td>{item.url}</td> 
          </tr> 
         )) 
       } 
       </tbody> 
      </Table> 
    } 
} 

export default connect()(ListingTable) 

app.js

import { ListingTable } from './component/ListingTable'; 
import { Provider } from 'react-redux' 
import {createStore} from 'redux'; 
import reducer from '../reducer/ListingReducer' 

export default class Page extends React.Component { 
    render() { 
     <Provider store={createStore(reducer)}> 
     <ListingTable data={this.props.item}/> 
     </Provider> 
    } 
} 

我看到存儲對象是目前在調試控制檯,但它不會傳遞到ListingTable組件:

enter image description here

+0

'進口ListingTable從上而'進口{} ListingTable默認出口exportclass ListingTable之前' ./''按名稱導入價格 – Hanmaslah

回答

7

您正在導入未連接的元件噸。

刪除

class ListingTable extends React.Component { 

    constructor(props) { 
    super(props); 
    this.handlers = createHandlers(this.props.dispatch); 
    } 
    render() { 
    return <Table> 
      <tbody> 
      { 
       this.props.data.map((item, key)=> 
        (
         // Undefined Error!!!! 
         <tr onClick={()=> this.handlers.onClick(item)} key={key}> 

          <td>{item.user_name}</td> 
          <td>{item.name}</td> 
          <td>{item.url}</td> 
         </tr> 
        )) 
      } 
      </tbody> 
     </Table> 
    } 
} 

export default connect()(ListingTable) 

app.js從 './ '`進口值

import ListingTable from './component/ListingTable'; 
+1

對於typescript/ES6,我還必須將我的導入從 'import {ListingTable}從'..''更改爲'從'..''導入ListingTable。 非常微妙,我想現在我知道'export default'如何工作。 – nimda

相關問題