我正在尝试将从ExportReportRoomSelectionModal
传递过来的道具设置为ExportModal
中的状态。在道具中有一个数组selectedRooms
。当我console.log(this.props.selectedRooms)
。我可以看到该数组已作为道具<ExportModal selectedRooms={this.state.selectedRooms}
传递,但是当我将其设置为在ExportModal.js
上声明为roomIdNo: props.selectedRooms || []
时,它似乎没有注册,因此{{1}的值}在roomId
上保持空白或为空。
ExportModal.js
export default class ExportReportRoomSelectionModal extends React.Component {
constructor(props) {
super(props);
const roomOrder = configContext.value.roomOrder;
this.state = {
rooms: roomOrder,
selectedRooms: []
};
this.onSelectRooms = this.onSelectRooms.bind(this);
}
onSelectRooms = (e) => {
const { selectedRooms } = this.state;
const { id } = e.target;
//remove room
if(selectedRooms.includes(id)) {
const newSelectedRooms = selectedRooms.filter((name) => name !== id);
this.setState({
selectedRooms: newSelectedRooms
});
//add room
} else{
this.setState({
selectedRooms: [...selectedRooms, id]
});
}
}
render() {
return (
<Modal >
<Modal.Header closeButton>
<Modal.Title>Print PDF</Modal.Title>
</Modal.Header>
<Modal.Body>
<p>Please select rooms</p>
<Grid fluid={true}>
<Row className="show-grid">
{ this.state.rooms.map((roomName, i ) =>
<Col key={i} xs={2} md={2}>
<Panel
onClick={this.onSelectRooms}>
<Panel.Heading id={roomName}>
{roomName}
</Panel.Heading>
</Panel>
</Col>
)}
</Row>
</Grid>
</Modal.Body>
<Modal.Footer>
<Button
bsStyle="primary"
onClick={this.props.handleHide}
data-toggle="modal"
data-dismiss="modal"
data-target="#export-modal"
>
Send PDF
</Button>
<Button bsStyle="primary" >Close</Button>
</Modal.Footer>
<ExportModal selectedRooms={this.state.selectedRooms} />
</Modal>);
}
}
答案 0 :(得分:1)
在.map()迭代中使用您的ID,并返回回调以修改所传递的ID值。
export default class ExportReportRoomSelectionModal extends React.Component {
constructor(props) {
super(props);
if (Array.isArray(props.roomOrderInit))
throw new Error("Room order is not an array");
this.state = {
rooms: props.roomOrderInit,
selectedRooms: props.selectedRoomsInit || []
};
this.onSelectRooms = this.onSelectRooms.bind(this);
}
onSelectRooms(id) {
return () => {
const { selectedRooms } = this.state;
//remove room
if (selectedRooms.includes(id)) {
const newSelectedRooms = selectedRooms.filter((name) => name !== id);
this.setState({
selectedRooms: newSelectedRooms
});
//add room
} else {
this.setState({
selectedRooms: [...selectedRooms, id]
});
}
}
}
render() {
return (
<Modal >
<Modal.Header closeButton>
<Modal.Title>Print PDF</Modal.Title>
</Modal.Header>
<Modal.Body>
<p>Please select rooms</p>
<Grid fluid={true}>
<Row className="show-grid">
{this.state.rooms.map((roomName, i) =>
<Col key={i} xs={2} md={2}>
<Panel
onClick={this.onSelectRooms(roomName)}
bsStyle={this.state.selectedRooms.includes(roomName) ? 'success' : 'default'} >
<Panel.Heading id={roomName}>
{roomName}
</Panel.Heading>
</Panel>
</Col>
)}
</Row>
</Grid>
</Modal.Body>
<Modal.Footer>
<Button
bsStyle="primary"
onClick={this.props.handleHide}
data-toggle="modal"
data-dismiss="modal"
data-target="#export-modal"
>
Send PDF
</Button>
<Button bsStyle="primary" >Close</Button>
</Modal.Footer>
<ExportModal selectedRooms={this.state.selectedRooms} />
</Modal>);
}
}