我搜索了建议,但找不到任何答案。我基本上认为我可以正确键入 HOC,如下所示:
这是我目前的组件:
// @flow
import React, { Component } from 'react';
import moment from 'moment';
import type { ApolloClient } from 'apollo-client';
import { convertDatesToISO } from 'components/Calendar/utils';
type Props = {
client: ApolloClient<any>,
location: {
search: string,
},
};
type SelectedDates = {
startOn: moment,
endOn: moment,
};
const withInitialSelectedDates = (WrappedComponent: Component<Props>): Component => {
return class extends Component<Props> {
initialSelectedDates: ?SelectedDates;
initialSelectedDatesFromQueryString(): ?SelectedDates {
const searchString = this.props.location.search;
const searchParams = new URLSearchParams(searchString);
const startOn = moment.utc(searchParams.get('start_date'));
const endOn = moment.utc(searchParams.get('end_date'));
if (!startOn.isValid() || !endOn.isValid()) return null;
if (startOn < moment.utc().startOf('day')) return null;
if (endOn < startOn) return null;
return { startOn, endOn };
}
setInitialSelectedDatesOnGraphQLClient(): void {
if (this.initialSelectedDates == null) return;
this.props.client.writeData({
data: {
selectedDates: convertDatesToISO([this.initialSelectedDates]),
},
});
}
componentDidMount(): void {
this.initialSelectedDates = this.initialSelectedDatesFromQueryString();
this.setInitialSelectedDatesOnGraphQLClient();
}
render(): React.Element {
return (
<WrappedComponent
initialSelectedDates={this.initialSelectedDates}
{...this.props}
/>
);
}
};
};
export default withInitialSelectedDates;
我想我可以改变:
const withInitialSelectedDates = (WrappedComponent: Component<Props>): Component => {
对此:
const withInitialSelectedDates = <PassedProps: {} & Props>(WrappedComponent: ComponentType<PassedProps>): ComponentType<PassedProps> => {
它将需要导入 ComponentType。我的问题是我应该在哪里更改我当前的代码并添加 PassedProps?