在我的 React 应用程序中,我有一个通过 api 响应获得的对象数组。我想在手风琴中显示每个对象的细节。我正在使用 react-accessible 手风琴并创建了一个 React 无状态组件。我希望我的每个手风琴都代表数组的一个对象。我在 dataProp 中有我的对象数组并迭代我已经编写了我的组件,如下所示 -
import React from 'react';
import ReactDOM from 'react-dom';
import ChildAccordion from './ChildAccordion'
import { setData } from '../actions/action'
import { connect } from 'react-redux'
import {
Accordion,
AccordionItem,
AccordionItemTitle,
AccordionItemBody,
} from 'react-accessible-accordion';
import 'react-accessible-accordion/dist/fancy-example.css';
import 'react-accessible-accordion/dist/minimal-example.css';
class ParentAccordion extends React.Component {
componentWillMount() {
//call to action
this.props.setData();
}
getMappedData = (dataProp) =>{
if (dataProp) {
return dataProp.map(item =>{
return <div>{dataProp[item]}</div>
})
}
else {
return "";
}
}
render(){
const { dataProp } = this.props;
return (
// RENDER THE COMPONENT
<Accordion>
<AccordionItem>
<AccordionItemTitle>
<h3>Details:
{
this.getMappedData(item[name])
}
</h3>
</AccordionItemTitle>
<AccordionItemBody>
<ChildAccordion {...dataProp} />
</AccordionItemBody>
</AccordionItem>
</Accordion>
);
}
}
const mapStateToProps = state => {
return {
dataProp: state.dataProp
}
};
const mapDispatchToProps = dispatch => ({
setData(data) {
dispatch(setData(data));
}
})
export default connect (mapStateToProps,mapDispatchToProps) (ParentAccordion)
这样做时,这给了我错误-
Uncaught ReferenceError: item is not defined
有人可以让我知道我哪里出错了吗?提前致谢。