回首憶惘然
2022-06-09 11:09:46
我與 TypeError 斗爭(zhēng):deleteEducation 不是一個(gè)函數(shù) - 2 個(gè) React 組件中的相同函數(shù)。該組件有效。 import React, { Fragment } from 'react' import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import Moment from 'react-moment'; import { deleteEducation } from '../../actions/profile'; export const Education = ({ education, deleteEducation }) => { const educations = education.map(edc => ( <tr key={edc._id}> <td> <button className='btn btn-danger' onClick={() => deleteEducation(edc._id)} >Delete</button> </td> </tr> )); return ( <Fragment> <h2 className='my-2'>Education Credentials</h2> <table className="table"> <tbody> {educations} </tbody> </table> </Fragment> ) } Education.propTypes = { education: PropTypes.array.isRequired, deleteEducation: PropTypes.func.isRequired, } export default connect(null, { deleteEducation })(Education);這沒有。我想使用另一種不同的方法來刪除Experience()。它不起作用,所以我嘗試了相同的功能,但組件名稱不同。import React, { Fragment } from 'react'import PropTypes from 'prop-types';import { connect } from 'react-redux';import Moment from 'react-moment';import { deleteEducation } from '../../actions/profile';export const Experience = ({ education, deleteEducation }) => { const educations = education.map(edc => ( <tr key={edc._id}> <td> <button className='btn btn-danger' onClick={() => deleteEducation(edc._id)} >Delete</button> </td> </tr> )); return ( <Fragment> <h2 className='my-2'>Education Credentials</h2> <table className="table"> <tbody> {educations} </tbody> </table> </Fragment> )}
2 回答

拉丁的傳說
TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊
您需要在兩個(gè)組件的連接包裝器中使用參數(shù)分派您的函數(shù)。
改變
export default connect(null, { deleteEducation })(Experience);
至
const mapDispatchToProps = dispatch => ({
deleteEducation: id => dispatch(deleteEducation(id))
})
export default connect(null, mapDispatchToProps)(Experience);

烙印99
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊
我的導(dǎo)入錯(cuò)誤,未包含在我的帖子中。抱歉 React 自動(dòng)添加了默認(rèn)導(dǎo)入,我沒有注意到。
import { Experience } from './Experience'; import Education from './Education';
添加回答
舉報(bào)
0/150
提交
取消