2

我搜索了建议,但找不到任何答案。我基本上认为我可以正确键入 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?

4

1 回答 1

0

您将需要遵循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}
        />
      );
    }
  }
}

尝试流

于 2019-05-16T15:12:23.663 回答