【发布时间】:2020-02-10 09:32:05
【问题描述】:
我在将一个方法向下传递到几个较低的子组件时遇到问题。我需要在父组件中保持状态,因此将方法向下传递以更改父组件中的状态值的原因。现在我知道这个问题之前已经回答过好几次了,但是我读到的所有内容都不起作用?
这是我想要做的:
父方法代码(OrderView.js):
handleChargesDateChange = (e) => {
const date = e.target.value;
const chargeId = e.target.name;
console.log(date);
this.setState({
updatedChargeDates: [
...this.state.updatedChargeDates,
{[chargeId]: date}
]
})
}
然后,此方法在 render() 中作为 props 传递:
<Grid.Row>
<Grid.Column width={16}>
<OrderSummary
order={this.state.order}
fuelTypes={this.state.fuelTypes}
vehicleClasses={this.state.vehicleClasses}
deviceTypes={this.state.deviceTypes}
chargeTypes={this.state.chargeTypes}
featureTypes={this.state.featureTypes}
itemGroups={this.state.itemGroups}
itemTypes={this.state.itemTypes}
**updateChargeDates={this.handleChargesDateChange}**
/>
</Grid.Column>
</Grid.Row>
然后在OrderSummary 中,我收到如下的道具并再次将其传递给另一个子组件:
const updateChargeDates = this.props;
console.log("Itemsssss " + this.props.order.items);
const panes = [];
if (this.props.order.devices.length > 0) {
panes.push({
menuItem: 'Devices', render: () => <DevicesTab
devices={this.props.order.devices || []}
fuelTypes={this.props.fuelTypes || []}
vehicleClasses={this.props.vehicleClasses || []}
deviceTypes={this.props.deviceTypes || []}
chargeTypes={this.props.chargeTypes || []}
featureTypes={this.props.featureTypes || []}
/>
});
}
if (this.props.order.items.length > 0) {
panes.push({
menuItem: 'Additional Items', render: () => <ItemsTab
items={this.props.order.items || []}
itemGroups={this.props.itemGroups || []}
itemTypes={this.props.itemTypes || []}
chargeTypes={this.props.chargeTypes || []}
**updateChargeDates={updateChargeDates}**
/>
})
}
然后,我将它传递给另一个子组件到 ItemsTab 组件中:
render() {
const { items, itemGroups, itemTypes, chargeTypes, updateChargeDates } = this.props;
return <Tab.Pane className='no-border'>
{items.length > 0 && <Grid>
<Grid.Row columns={2}>
<Grid.Column>
<ItemGroup
itemOrder={items[this.state.currentItem] || {}}
itemGroups={itemGroups || []}
itemTypes={itemTypes || []}
/>
</Grid.Column>
<Grid.Column>
<Charges
charges={items[this.state.currentItem].charges || {}}
chargeTypes={chargeTypes || []}
handleDateChange={this.handleChange}
**updateChargeDates={updateChargeDates}**
/>
</Grid.Column>
</Grid.Row>
最后,在Charges 组件中,我尝试使用该方法来简单地更新日期:
render() {
const {charges, chargeTypes, updateChargeDates} = this.props;
if (charges.length === 0) { return null; }
return <Form>
<h3>Charges</h3>
{charges.map(charge => {
console.log(charge);
const chargeType = chargeTypes.find(type => type.code === charge.type) || {};
return <Grid>
<Grid.Row columns={2}>
<Grid.Column>
<Form.Input
key={charge.id}
label={chargeType.description || charge.type}
value={Number(charge.amount).toFixed(2)}
readOnly
width={6} />
</Grid.Column>
<Grid.Column>
<DateInput
name={charge.id}
selected={this.state.newDate}
**onChange={updateChargeDates}**
value={charge.effectiveDate}
/>
</Grid.Column>
</Grid.Row>
</Grid>
})}
<Divider hidden />
<br />
</Form>
}
注意:我用** 突出显示了我关心的代码位
我在浏览器控制台中收到的错误如下:
现在我知道,当你没有 bind 类的方法时,通常会发生此错误,但我使用的是 => ES6 语法,我认为不需要这样做?我什至尝试将绑定添加到父组件,如下所示:
this.handleChargesDateChange = this.handleChargesDateChange.bind(this);
但这又是行不通的。我尝试了以下资源,但没有运气:
有人可以解释一下我哪里出错了吗?我现在尝试了几个小时,但没有运气:/
谢谢!
【问题讨论】:
标签: reactjs