我在我的渲染函数中有一个带有open_hours的列表。我将它们映射到一个子组件。当子组件状态更改时,我想更改父状态内每个对象的值。唯一的问题是我的值在子组件或父状态中都没有改变。我已经将onChange方法传递给每个子组件,但是问题出在父状态下的onChange函数上。
我尝试将onChange传递给我的子组件,并使用[e.target.name]:e.target.value更新状态。但这没有用。所以我试图复制arrayList并根据映射的arrayList的索引更新状态。那也没有用。
handleOnChange = (index) => {
let newData = [...this.state.opening_hours];
this.setState(
newData.map((barbershop, j) => {
if (j === index) {
return barbershop;
console.log(barbershop)
}
return {
newData,
};
}));
console.log(newData)
};
render() {
return (
<>
<div className="container mb-2">
<h4>Opening hours </h4>
<Form>
{this.state.opening_hours.map((barbershop, index) => (
<Day
key={barbershop.id}
day={barbershop.day}
open_time={barbershop.open_time}
close_time={barbershop.close_time}
index = {index}
onChange={() => this.handleOnChange(index)}/>
))}
</Form>
<Button onClick={this.handleSubmit} variant="primary" type="submit">
Update opening hours
</Button>
</div>
</>
);
}
}
我的孩子部分看起来像这样
class Day extends Component {
constructor(props) {
super(props);
this.state = {
day: this.props.day,
open_time: this.props.open_time,
close_time: this.props.close_time,
};
console.log(this.state)
}
handleChange = () => {
this.props.onChange(this.props.index)
}
render() {
return (
<Form.Group as={Row}>
<Form.Label column sm="3">
{ENUM[this.state.day]}
</Form.Label>
<Col sm="3">
<Form.Control
name="open_time"
value={this.state.open_time}
onChange={this.handleChange}
type="time"
min="08:00"
max="24:00"/>
</Col>
<Col sm="3">
<Form.Control
name="close_time"
value={this.state.close_time}
onChange={this.handleChange}
type="time"
min="08:00"
max="24:00"/>
</Col>
</Form.Group>
);
}
}
编辑:
最佳答案
因此,我进行了一些调查,我相信问题是您没有将数据返回给使用者。
我认为这是解决方案:
import React, { Component, useState, useEffect } from "react";
class UI extends Component {
handleOnChange = ({ index, ...newHours }) => {
// update array item
const opening_hours = this.state.opening_hours.map((item, i) => {
const newData = i === index ? newHours : {};
return {
...item,
...newData
};
});
// update item in opening_hours => state
this.setState({ opening_hours });
};
render() {
return (
<div className="container mb-2">
<h4>Opening hours</h4>
<Form>
{this.state.opening_hours.map(({ id, ...barbershop }, index) => (
<Day
key={id}
{...barbershop}
{...{ index }}
onChange={this.handleOnChange}
/>
))}
</Form>
<Button onClick={this.handleSubmit} variant="primary" type="submit">
Update opening hours
</Button>
</div>
);
}
}
const Day = ({ day, onChange, index, ...props }) => {
const [open_time, setOpenTime] = useState(props.open_time);
const [close_time, setCloseTime] = useState(props.close_time);
useEffect(() => {
onChange({ index, open_time, close_time });
}, [open_time, close_time, onChange, index]);
const sharedProps = {
type: "time",
min: "08:00",
max: "24:00"
};
return (
<Form.Group as={Row}>
<Form.Label column sm="3">
{ENUM[day]}
</Form.Label>
<Col sm="3">
<Form.Control
{...sharedProps}
name="open_time"
value={open_time}
onChange={({ target }) => setOpenTime(target.value)}
/>
</Col>
<Col sm="3">
<Form.Control
{...sharedProps}
name="close_time"
value={close_time}
onChange={({ target }) => setCloseTime(target.value)}
/>
</Col>
</Form.Group>
);
};