如果我在中间写入,则单击适当的按钮后,所选食物的类型会成功显示在模态中 <Modal>
<p>{this.state.cTaco}</p>
<p>{this.state.bTaco}</p>
<p>{this.state.cBurrito}</p>
但我希望它使用某种地图或循环动态显示,而不是一一写出。
使用我当前的代码,它在模态上显示对象中项目的所有名称,而不是实际的食物名称。
cTacoClicked
cTaco
bTacoClicked
bTaco
cBurritoSelected
cBurrito
它应该显示(同样,如果我将状态 1 by 1 写出,它确实会像这样显示):
Chicken Taco
Beef Taco
Chicken Burrito
我如何正确使用循环来动态实现这一点。
这是我的代码:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import Modal from 'react-modal';
import Aux from '../../../../hoc/Aux';
import FoodButton from '../FoodButtons/FoodButton';
import CheckoutButton from '../CheckoutButton/CheckoutButton';
import axios from '../../../../axios-foodChosen';
import { CLOSE_MODAL, OPEN_MODAL } from "../../../../store/action/NoNameAction";
class TacoTypes extends Component {
state = {
items: {
cTacoClicked: false,
cTaco: '',
bTacoClicked: false,
bTaco: '',
cBurritoSelected: false,
cBurrito: ''
}
}
componentWillMount() {
// for modal
Modal.setAppElement('body');
}
chickenTaco() {
// modal
const cTacoSelected = "Chicken Taco";
this.setState({cTacClicked: true, cTaco: cTacoSelected});
}
beefTaco() {
// modal
const bTacoSelected = "Beef Taco";
this.setState({bTacoClicked: true, bTaco: bTacoSelected});
}
chickenBurrito() {
// modal
const cBurritoSelected = "Chicken Burrito";
this.setState({cBurritoSelected: true, cBurrito: cBurritoSelected });
}
render() {
return (
<Aux>
<FoodButton clicked={() => this.chickenTaco()} label={"Chicken Taco"}/>
<FoodButton clicked={() => this.beefTaco()} label={"Beef Taco"}/>
<FoodButton clicked={() => this.chickenBurrito()} label={"Chicken Burrito"}/>
<CheckoutButton clicked={() => this.props.openModalRedux()}/>
<Modal isOpen={this.props.isOpen}>
<p>
{
Object.keys(this.state.items).map(i => (
<p>{i}</p>
))
}
</p>
<button onClick={() => this.props.closeModalRedux()}>Close</button>
</Modal>
</Aux>
);
}
}
const mapStateToProps = state => {
return {
// props for modal
isOpen: state.global.isModalOpen,
}
};
const mapDispatchToProps = dispatch => {
return {
// Modal handlers
openModalRedux: () => dispatch({type: OPEN_MODAL}),
closeModalRedux: () => dispatch({type: CLOSE_MODAL})
}
};
export default connect(mapStateToProps, mapDispatchToProps)(TacoTypes);