用redux中的dispatch()和connect()解释这个问题吗?

我正在观看本教程,并且下面的代码在视频中可以正常运行,但是当我尝试它时,出现“未定义派遣”错误。为什么会这样?

import React from 'react'
import {connect} from 'react-redux'
import {removeExpense} from '../actions/actions'

const ListItem = ({id,description,note,amount,createdAt})=>(
    <div>
        <h5>{description}</h5>
        <p>Note: {note}</p>
        <p>Amount: {amount}</p>
        <p>Created @{createdAt}</p>
        <button onClick={()=>{dispatch(removeExpense(id))}}>remove</button>
        <br/>
    </div>
)

export default connect()(ListItem); //Just to inject dispatch() 

讲师正在使用redux v3.7.2,react-redux v5.0.5 react v15。 现在我知道,如果按以下方式进行处理,就可以解决问题。

const ListItem = (props)=>(
    <div>
        <h5>{props.description}</h5>
        <p>Note: {props.note}</p>
        <p>Amount: {props.amount}</p>
        <p>Created @{props.createdAt}</p>
        <button onClick={()=>{props.dispatch(removeExpense(props.id))}}>remove</button>
        <br/>
    </div>
)

为什么在第一个代码中不能删除按钮?可能是这些库的过时版本吗?

red2yuri 回答:用redux中的dispatch()和connect()解释这个问题吗?

您可以通过更改以下行来修正第一个示例:

const ListItem = ({id,description,note,amount,createdAt})=>(

const ListItem = ({id,createdAt,dispatch})=>(
,

在破坏道具时,您省略了本应传递的调度。

将其添加到您的销毁语句中

const ListItem = ({id,dispatch})=>(

或通过props

访问它
<button onClick={()=>{props.dispatch(removeExpense(id))}}>remove</button>
本文链接:https://www.f2er.com/3134887.html

大家都在问