gpt4 book ai didi

javascript - 在 react 组件之间传递数据

转载 作者:行者123 更新时间:2023-12-03 13:36:51 27 4
gpt4 key购买 nike

我正在尝试制作简单的日历。在我的日历中,我有日期、日期名称、月份和月份名称组件,并且我在主日历上访问它。初始加载时,它将显示当前月历。

现在,当我从月份名称组件中选择月份时,希望传递到月份组件,然后传递到日期组件以呈现日期。

我尝试使用回调函数,但它没有帮助我

整个 react 组件都在这里 https://stackblitz.com/edit/react-ts-z2278r

日组件

import * as moment from 'moment'
import * as React from 'react'

import { showComponent } from '../../../../models'
import { MonthNames } from './'
import styles from './Calendar.module.scss'

interface IDatePickerMonthsProps {
changeMonthComponent: (showComponent: showComponent, monthNo: number) => void
}

interface IDatePickerMonthsState {
dateObject: moment.Moment,
monthNo?: number
}

export class DatePickerMonths extends React.Component<IDatePickerMonthsProps, IDatePickerMonthsState> {

constructor(props: IDatePickerMonthsProps) {
super(props)
this.state = {
dateObject: moment()
}
}

public render() {
const monthPicker =
<div className="datepicker-days">
<table className={styles.table}>
<thead>
<tr>
<th><span className="ms-Icon ms-Icon--ChevronLeft" title="Previous Month" onClick={this.onPrev}></span></th>
<th className={styles["picker-switch"]} data-action="pickerSwitch" colSpan={5} title="Select Month">
{this.year()}
</th>
<th><span className="ms-Icon ms-Icon--ChevronRight" title="Next Month" onClick={this.onNext}></span></th>
</tr>
</thead>
<tbody>
<tr>
<td colSpan={7}>
<MonthNames changeMonthComponent={() => this.showDays(this.state.monthNo)} />
</td>
</tr>
</tbody>
</table>
</div>

return (monthPicker)
}

private year = () => {
return this.state.dateObject.format("Y");
};

private onPrev = () => {
this.setState({
dateObject: this.state.dateObject.subtract(1, "year")
});
};
private onNext = () => {
this.setState({
dateObject: this.state.dateObject.add(1, "year")
});
};

private showDays = (monthNo: number) => {
console.log(monthNo)
this.setState({ monthNo: monthNo })
this.props.changeMonthComponent(showComponent.Days, monthNo)
}
}

月份名称组件

import * as moment from 'moment'
import * as React from 'react'

import { showComponent } from '../../../../models'

interface IMonthNamesProps {
changeMonthComponent: (showComponent: showComponent, monthNo: number) => void
}

export class MonthNames extends React.Component<IMonthNamesProps, {}> {
private monthshort: string[] = moment.monthsShort();

constructor(props: IMonthNamesProps) {
super(props)
}

public render() {
let monthshortname = this.monthshort.map((month, monthNo) => {
return <span key={monthNo} onClick={() => this.showDays(monthNo)}>{month}</span>;
});
return monthshortname
}

private showDays = (monthNo: number) => {
this.props.changeMonthComponent(showComponent.Days, monthNo)
}
}

月份组件

import * as moment from 'moment'
import * as React from 'react'

import { showComponent } from '../../../../models'
import { MonthNames } from './'
import styles from './Calendar.module.scss'

interface IDatePickerMonthsProps {
changeMonthComponent: (showComponent: showComponent, monthNo: number) => void
}

interface IDatePickerMonthsState {
dateObject: moment.Moment,
monthNo?: number
}

export class DatePickerMonths extends React.Component<IDatePickerMonthsProps, IDatePickerMonthsState> {

constructor(props: IDatePickerMonthsProps) {
super(props)
this.state = {
dateObject: moment()
}
}

public render() {
const monthPicker =
<div className="datepicker-days">
<table className={styles.table}>
<thead>
<tr>
<th><span className="ms-Icon ms-Icon--ChevronLeft" title="Previous Month" onClick={this.onPrev}></span></th>
<th className={styles["picker-switch"]} data-action="pickerSwitch" colSpan={5} title="Select Month">
{this.year()}
</th>
<th><span className="ms-Icon ms-Icon--ChevronRight" title="Next Month" onClick={this.onNext}></span></th>
</tr>
</thead>
<tbody>
<tr>
<td colSpan={7}>
<MonthNames changeMonthComponent={() => this.showDays(this.state.monthNo)} />
</td>
</tr>
</tbody>
</table>
</div>

return (monthPicker)
}

private year = () => {
return this.state.dateObject.format("Y");
};

private onPrev = () => {
this.setState({
dateObject: this.state.dateObject.subtract(1, "year")
});
};
private onNext = () => {
this.setState({
dateObject: this.state.dateObject.add(1, "year")
});
};

private showDays = (monthNo: number) => {
console.log(monthNo)
this.setState({ monthNo: monthNo })
this.props.changeMonthComponent(showComponent.Days, monthNo)
}
}

最佳答案

请参阅此位:<MonthNames changeMonthComponent={() => this.showDays(this.state.monthNo)} />

还有这一点:this.props.changeMonthComponent(showComponent.Days, monthNo)

问题在于内部组件调用函数changeMonthComponent,但参数被父组件丢弃。 MonthNames 组件需要对传回的参数执行一些操作:

<MonthNames changeMonthComponent={(days, monthNo) => this.showDays(monthNo)} />

(粗略地 - 我没有深入研究代码的所有方面,因此我不知道您到底想如何处理我的建议忽略的 days 参数。)

关于javascript - 在 react 组件之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59781640/

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