gpt4 book ai didi

javascript - 当我尝试将变量传递给 react 模式时, props 的值为空

转载 作者:行者123 更新时间:2023-12-02 22:30:04 25 4
gpt4 key购买 nike

所以我正在工作中构建一个库存系统,并且我有一个列出所有库存项目的表格。然后,我可以单击表的一行并检索有关该特定项目的信息。我想要实现的是一个弹出该项目信息的模式,但我遇到的问题是由于某种原因没有信息传递到我的模式。

当我单击项目表中的一行并控制台记录结果时,我会返回模式应包含的必要字段,但当模式打开时,所有字段都是空的。

这是我的模式的代码

import React from 'react';

export default class InventoryModal extends React.Component{
constructor(props){
super(props);

this.state = {
Item: this.props.item,
ID: this.props.item.ID,
CreatedBy: this.props.item.ID,
DateCreated: this.props.item.DateCreated,
Description: this.props.item.Description,
Location: this.props.item.Location,
ModelNumber: this.props.item.ModelNumber,
Serial: this.props.item.Serial,
PartNumber: this.props.item.PartNumber,
PartName: this.props.item.PartName,
Price: this.props.item.Price,
QuantityOnHand: this.props.item.QuantityOnHand,
ReorderPoint: this.props.item.ReorderNumber,
Type: this.props.item.Type,
Units: this.props.item.Units,
Vendor: this.props.item.Vendor,
}
}

componentWillMount(){
console.log("item passed in: " + JSON.stringify(this.state.Item));
//this.PullDataFromDatabase();
}

PullDataFromDatabase(){
//console.log(this.state.Item);
var item = this.state.Item;
this.RenderContent(item);

this.setState({ID: item.ID,
CreatedBy: item.CreatedBy,
DateCreated: item.DateCreated,
Description: item.Description,
Location: item.Location,
ModelNumber: item.ModelNumber,
Serial: item.Serial,
PartNumber: item.PartNumber,
PartName: item.PartName,
Price: item.Price,
QuantityOnHand: item.QuantityOnHand,
ReorderPoint: item.ReorderPoint,
Type: item.Type,
Units: item.Units,
Vendor: item.Vendor,
})
}

OnChangeHandler(e){
switch(e.target.id){
case 'partname': this.setState({PartName: e.target.value})
break;
case 'description': this.setState({Description: e.target.value})
break;
case 'location': this.setState({Location: e.target.value})
break;
case 'modelnumber': this.setState({ModelNumber: e.target.value})
break;
case 'partnumber': this.setState({PartNumber: e.target.value})
break;
case 'serialnumber': this.setState({Serial: e.target.value})
break;
case 'price': this.setState({Price: e.target.value})
break;
case 'vendor': this.setState({Vendor: e.target.value})
break;
}
}

CloseHandler(){
document.getElementById('myModal').style.display = "none"
}

SaveHandler(){

}

render(){
return(
<div className="Modal-Background">
<div className="Inventory-Modal">
<button className="ButtonInv1" style={{position: 'absolute', left: '95%', bottom: '93%', width: '35px'}} onClick={this.CloseHandler.bind(this)}>X</button>
<p style={{fontSize: "20px", borderBottom: '2px solid black', width: '33%', marginLeft: '33%', marginTop: '0', marginBottom: '2%'}}><b>Part Information</b></p>
<div className="ModalContent">
<div id="c1" style={{width: '33%', display: 'flex', marginTop: '4%'}}>
<div className="C1-1">
<p style={{margin: '0', marginBottom: '2%'}}>Part Name</p>
<br />
<p style={{margin: '0', marginBottom: '2%'}}>Type</p>
<br />
<p style={{margin: '0', marginBottom: '2%'}}>Vendor</p>
<br />
<p style={{margin: '0', marginBottom: '2%'}}>Location</p>
</div>
<div className="C1-2">
<input style={{textAlign: 'center'}} type="text" id="partname" value={this.state.Partname} onChange={this.OnChangeHandler.bind(this)}></input>
<br />
<br />
<input style={{textAlign: 'center'}} id="type" disabled="true" value={this.state.Type}></input>
<br />
<br />
<input style={{textAlign: 'center'}} id="vendor" onChange={this.OnChangeHandler.bind(this)} value={this.state.Vendor}></input>
<br />
<br />
<input style={{textAlign: 'center'}} id="location" onChange={this.OnChangeHandler.bind(this)} value={this.state.Location}></input>
</div>
</div>

<div id="c2" style={{width: '33%'}}>
<p>Date Created</p>
<input style={{textAlign: 'center'}} id="datecreated" disabled="true" value={this.state.DateCreated}></input>
<p>Part Number</p>
<input style={{textAlign: 'center'}} id="partnumber" onChange={this.OnChangeHandler.bind(this)} value={this.state.PartNumber}></input>
<p>Model Number</p>
<input style={{textAlign: 'center'}} id="modelnumber" onChange={this.OnChangeHandler.bind(this)} value={this.state.ModelNumber}></input>
<p>Serial Number</p>
<input style={{textAlign: 'center'}} id="serialnumber" onChange={this.OnChangeHandler.bind(this)} value={this.state.Serial}></input>
<p style={{marginTop: '25%'}}>Description</p>
<textarea style={{width: '100%', height: '85px'}} id="description" onChange={this.OnChangeHandler.bind(this)} value={this.state.Description}></textarea>
</div>

<div id="c3" style={{width: '33%', display: 'flex', marginTop: '4%'}}>
<div className="C3-2">
<input style={{textAlign: 'center'}} type="text" id="quantity" disabled="true" value={this.state.QuantityOnHand}></input>
<br />
<br />
<input style={{textAlign: 'center'}} id="price" onChange={this.OnChangeHandler.bind(this)} value={this.state.Price}></input>
<br />
<br />
<input style={{textAlign: 'center'}} id="units" disabled="true" value={this.state.Units}></input>
<br />
<br />
<input style={{textAlign: 'center'}} id="reorderpoint" onChange={this.OnChangeHandler.bind(this)} value={this.state.ReorderPoint}></input>
</div>
<div className="C3-1">
<p style={{margin: '0', marginBottom: '2%'}}>QTY</p>
<br />
<p style={{margin: '0', marginBottom: '2%'}}>Price</p>
<br />
<p style={{margin: '0', marginBottom: '2%'}}>Units</p>
<br />
<p style={{margin: '0', marginBottom: '2%'}}>Reorder Point</p>
</div>

<button className="ButtonInv1" style={{position: 'relative', top: '80%', right: '2%'}} onClick={this.SaveHandler.bind(this)}>Save</button>
</div>
</div>
</div>
</div>
)
}
}

这是调用我的模态的代码

import React from 'react'

class ViewAllItems extends React.Component{
constructor(props){
super(props);

this.state = {
ItemList: this.props.ItemList,
updateClickedRow: "",
ItemToPass: "",
ItemSelected: "",
}
}
RowClick(e){
//console.log(e.target.id)
var items = this.state.ItemList;

for(let i = 0; i < items.length; i++){
if(items[i].ID == e.target.id){
var Item = {
ID: items[i].ID,
CreatedBy: items[i].CreatedBy,
DateCreated: items[i].DateCreated,
Description: items[i].Description,
Location: items[i].Location,
ModelNumber: items[i].ModelNumber,
Serial: items[i].Serial,
PartNumber: items[i].PartNumber,
PartName: items[i].PartName,
Price: items[i].Price,
QuantityOnHand: items[i].QuantityOnHand,
ReorderPoint: items[i].ReorderPoint,
Type: items[i].Type,
Units: items[i].Units,
Vendor: items[i].Vendor,
}
}
}

this.setState({ updateClickedRow: e.target.id,
ItemToPass: Item,
}); //Updates Left Column
(document.getElementById('myModal')).style.display = "block";
}

RenderTable2(){
var items = this.state.ItemList;
var rows = [];


for(let i = 0; i < items.length; i++){
if(this.state.ItemSelected == "All Items"){
rows.push(
<tr style={{backgroundColor: '#B7BCDF'}} id={items[i].ID} onClick={this.RowClick.bind(this)} key={i}>
<td style={{width: '111px'}} id={items[i].ID} onClick={this.RowClick.bind(this)}>
{items[i].PartName}
</td>

<td style={{width: '94px'}} id={items[i].ID} onClick={this.RowClick.bind(this)}>
{items[i].Vendor}
</td>

<td style={{width: '100px'}} id={items[i].ID} onClick={this.RowClick.bind(this)}>
{items[i].PartNumber}
</td>

<td style={{width: '100px'}} id={items[i].ID} onClick={this.RowClick.bind(this)}>
{items[i].Price}
</td>

<td style={{width: '100px'}} id={items[i].ID} onClick={this.RowClick.bind(this)}>
{items[i].QuantityOnHand}
</td>
</tr>
)
}else if(this.state.ItemSelected == "Assembly Items"){
if(items[i].Type == "Assembly Item"){
rows.push(
<tr style={{backgroundColor: '#B7BCDF'}} id={items[i].ID} onClick={this.RowClick.bind(this)} key={i}>
<td style={{width: '111px'}} id={items[i].ID} onClick={this.RowClick.bind(this)}>
{items[i].PartName}
</td>

<td style={{width: '94px'}} id={items[i].ID} onClick={this.RowClick.bind(this)}>
{items[i].Vendor}
</td>

<td style={{width: '100px'}} id={items[i].ID} onClick={this.RowClick.bind(this)}>
{items[i].Location}
</td>

<td style={{width: '100px'}} id={items[i].ID} onClick={this.RowClick.bind(this)}>
{items[i].PartNumber}
</td>

<td style={{width: '100px'}} id={items[i].ID} onClick={this.RowClick.bind(this)}>
{items[i].QuantityOnHand}
</td>
</tr>
)
}
}

}

return (

<div className="TableScroll">
<table className="TableRows">
<tbody>
{rows}
</tbody>
</table>
</div>

);

}

RenderModal(){
//console.log(this.state.ItemToPass);
console.log("item passed to modal: " + JSON.stringify(this.state.ItemToPass));
let item = this.state.ItemToPass;

return(
<InventoryModal item={item} />
);
}

render(){
return(
<div className="ViewAllItemsContainer">
<div className="ViewAllDropdown">
<p style={{fontSize: "20px"}}><b>Select an Item</b></p>
<select style={{width: '200px', overflowY: 'scroll'}} id="ItemSelect" onChange={this.ItemSelected.bind(this)} value={this.state.ItemSelected}>
<option value="All Items"> All Items</option>
<option value="Assembly Items">Assembly Items</option>
{this.RenderOptions(this.props.ItemList)}
</select>
<br />
{this.KioskInput()}
</div>
<br />
<div className="CheckboxContainer"></div>

<div id="myModal" style={{display: 'none'}}>
{this.RenderModal(this)}
</div>


<div className="InvTable" style={{textAlign: 'center'}}>
{this.state.ItemSelected == "All Items" ? (
<table className="TableHeaderContainer" style={{textAlign: 'center'}}>
<thead>
<tr>
<th style={{position: "relative", left: '1%'}}>Part Name</th>
<th style={{position: "relative", left: '5%'}}>Vendor</th>
<th style={{position: "relative", left: '5%'}}>Part Number</th>
<th style={{position: "relative", left: '8%'}}>Price</th>
<th style={{position: "relative", left: '6%'}}>Quantity on Hand</th>
</tr>
</thead>
</table>
) :
this.state.ItemSelected == "Assembly Items" ? (
<table className="TableHeaderContainer" style={{textAlign: 'center'}}>
<thead>
<tr>
<th style={{position: "relative", left: '2%'}}>Part Name</th>
<th style={{position: "relative", left: '7%'}}>Vendor</th>
<th style={{position: "relative", left: '11%'}}>Location</th>
<th style={{position: "relative", left: '12%'}}>Part Number</th>
<th style={{position: "relative", left: '5%'}}>Quantity on Hand</th>
</tr>
</thead>
</table>
):
<table className="TableHeaderContainer">
<thead>
<tr>
<th style={{position: 'relative', right: '1%'}}>Part Number</th>
<th style={{position: 'relative', right: '2%'}}>Created Date</th>
<th style={{position: 'relative', right: '5%'}}>Serial Number</th>
<th style={{position: 'relative', right: '8%'}}>Model Number</th>
<th style={{position: 'relative', right: '7%'}}>Vendor</th>
<th style={{position: 'relative', right: '3%'}}>Firmware</th>
<th>Phone</th>
<th>Part Status</th>
<th style={{position: 'relative', left: '2%'}}>Kiosk</th>
<th style={{position: 'relative', left: '2%'}}>Last Changed By</th>
</tr>
</thead>
</table>
}

{this.RenderTable2(this)}
</div>
</div>
)



}
}

export default ViewAllItems

这可能不是解决此问题的最佳方法,但如果有人对为什么这不起作用有任何想法,我将不胜感激!谢谢。

最佳答案

当您的 item 属性更改时,您需要更新 InventoryModal 的状态。

您可以使用 componentDidUpdate 来执行此操作在您的 InventoryModal 中:

  componentDidUpdate(prevProps, prevState, snapshot) {
if (this.props.item && prevState.ID !== this.props.item.ID) {
const item = this.props.item;
this.setState({
Item: item,
ID: item.ID,
CreatedBy: item.CreatedBy,
DateCreated: item.DateCreated,
Description: item.Description,
Location: item.Location,
ModelNumber: item.ModelNumber,
Serial: item.Serial,
PartNumber: item.PartNumber,
PartName: item.PartName,
Price: item.Price,
QuantityOnHand: item.QuantityOnHand,
ReorderPoint: item.ReorderNumber,
Type: item.Type,
Units: item.Units,
Vendor: item.Vendor
});
}
}

关于javascript - 当我尝试将变量传递给 react 模式时, props 的值为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58941206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com