gpt4 book ai didi

reactjs - react : how to properly type the HOC component?

转载 作者:行者123 更新时间:2023-12-05 07:23:12 25 4
gpt4 key购买 nike

我搜索了这些建议,但找不到任何答案。我基本上认为我可以按如下方式正确输入 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?

最佳答案

您需要遵循 "Injecting Props" section 中的示例HOC Flow 文档。一个示例实现可能看起来像,

import * as React from 'react';

// Since Try Flow doesn't have access to these types
type ApolloClient<T> = any;
type moment = any;

type Props = {
client: ApolloClient<any>,
location: {
search: string,
},
};

type SelectedDates = {
startOn: moment,
endOn: moment,
};

function withInitialSelectedDates(
Component: React.AbstractComponent<Props>
): React.AbstractComponent<$Diff<Props, { initialSelectedDates: SelectedDates | void }>> {
return class WrappedComponent extends React.Component<
$Diff<Props, { initialSelectedDates: SelectedDates | void }>,
{ initialSelectedDates: SelectedDates | null }
> {
state = {
initialSelectedDates: null,
}

getInitialSelectedDatesFromQueryString(): SelectedDates | null {
if (true) {
return { startOn: 'start', endOn: 'end' };
} else {
return null;
}
// use actual implementation; I just needed to satisfy type check
}

setInitialSelectedDatesOnGraphQLClient(selectedDates: SelectedDates | null): void {
// implementation
}

componentDidMount(): void {
const initialSelectedDates = this.getInitialSelectedDatesFromQueryString();
this.setState({ initialSelectedDates });
this.setInitialSelectedDatesOnGraphQLClient(initialSelectedDates);
}

render() {
return (
<Component
{...this.props}
initialSelectedDates={this.state.initialSelectedDates}
/>
);
}
}
}

Try Flow

关于reactjs - react : how to properly type the HOC component?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56117398/

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