reactjs - ReactJS:如何使用道具设置状态?
问题描述
我正在尝试设置从传递ExportReportRoomSelectionModal
到状态的道具ExportModal
。在道具中有一个数组selectedRooms
。当我console.log(this.props.selectedRooms)
. 我可以看到数组已作为道具传递,<ExportModal selectedRooms={this.state.selectedRooms}
但是当我将它设置为状态时ExportModal.js
,roomIdNo: props.selectedRooms || []
它似乎没有注册,因此值roomId
保持为空或 null on 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>);
}
}
export default class ExportModal extends React.Component {
constructor(props) {
super(props);
this.state = {
roomIdNo: props.selectedRooms || [],
};
解决方案
在 .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>);
}
}
推荐阅读
- c# - 如何从我的非托管代码在主线程上执行某些操作?
- python - 使用 RFECV 和排列重要性的正确方法 - Sklearn
- python - 将带有字典的浮点数转换为新列表
- java - 在 JAVA 中转换西里尔字符
- c# - c# 应用程序创建 Unity 包
- sql - 如何在 SQL 查询中引用 Power BI 度量?
- python-3.x - 字典函数:这是什么意思
- amazon-web-services - 如何在 AWS 的 HttpApi 的 http 代理集成中使用 cognito:username?
- java - 启动 HornetQ 服务器时出现错误
- machine-learning - “生成的三元序列中 1 的数量”真的是一个合适的损失函数吗?