【发布时间】:2021-02-19 21:06:48
【问题描述】:
我正在使用react-week-scheduler 在我的页面上显示周历视图。该组件是基于类的父组件的功能子组件。我希望父组件管理日历上显示的事件的schedule 状态,并将其作为道具传递给子组件。
我面临的问题是,每次我对TimeGrid 进行调整(通过单击并拖出一个新事件块)并触发我的handleGridChange 函数以将该调整保存到schedule 状态字段,它会导致状态更新的无限循环,因为setState 导致子TimeGrid 组件更新,从而再次触发handleGridChange 函数,使我进入无限循环。
如果我直接在子组件上处理schedule 状态并使用useState 挂钩更新它,TimeGrid 组件可以正常工作,但是我的应用程序的其余部分无法访问schedule状态。我曾尝试将 TimeGrid 转换为基于类的组件,但后来我遇到了 setState 的其他问题,因为我不认为该组件是为这种方式设计的。
我对 react hooks 很陌生/不熟悉,所以我觉得这可能会导致这个问题的部分甚至全部。这也可能与TimeGrid 组件的设计方式有关,useEffect 在我设置状态时会不断改变事物,但我不确定也无法弄清楚。任何见解都会非常有帮助。
我的父组件看起来像这样:
import TimeGrid from './TimeGrid';
class ScheduleView extends Component {
constructor(props) {
super(props);
this.state = {
schedule: [], // initially empty
};
)
handleGridChange(newSchedule) {
this.setState({
schedule: newSchedule,
});
}
render() {
return (
<TimeGrid
schedule={this.state.schedule}
handleGridChange={this.handleGridChange.bind(this)}
/>
);
}
}
我的TimeGrid 组件看起来像这样:
function TimeGrid(props) {
// const [schedule, setSchedule] = useState([]); // <-- managing state with this works fine, but then I don't have access to it outside this component
// oldHandleGridChange(newSchedule) {
// setSchedule(newSchedule);
// }
return (
<TimeGridScheduler
schedule={props.schedule}
onChange={props.handleGridChange}
/>
);
}
export default TimeGrid;
【问题讨论】:
-
在构造函数上绑定函数,然后将函数传递给Child
标签: javascript reactjs