簡體   English   中英

在redux中沒有使用reactjs定義調度

[英]Dispatch is not defined in redux with reactjs

我想知道為什么點擊表格行仍然會給我未定義的調度錯誤導致Uncaught TypeError: dispatch is not a function 我一直在關注這篇文章 ,試圖解決問題,但到目前為止還沒有成功。

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組件:

在此輸入圖像描述

您正在導入未連接的組件。

class ListingTable之前刪除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';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM