2017-09-26 4 views
0

私はnext.jsページ用のHOCコンポーネントを作成しています。このHOCは、特定のgetInitialProps静的関数を持つコンポーネントを受け入れる必要があります。React.ComponentTypeを静的プロパティで入力する方法は?

私は流れと、このための右のタイピングを見つけ出すことができません:私はこのエラーを取得する

const wrapComponent = (Component: React.ComponentType<*>) => { 
    const original: Function = Component.getInitialProps; 

    return class extends React.Component<*> { 
     static async getInitialProps(ctx) { 
      const props = await original(ctx); 
      return { 
       ...props, 
       custom: 'a', 
      }; 
     } 

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

を:

5:  const original: Function = Component.getInitialProps; 
              ^property `getInitialProps`. Property not found in 
5:  const original: Function = Component.getInitialProps; 
           ^statics of React$Component 

Demo

+0

コンポーネントは 'getInitialProps'方法がありませんでし反応します。それらが特定のコンポーネントである場合、 'React.Component'ではなく' Component'をそのように入力する必要があります。 –

+0

@FelixKlingはクラスインターフェイスを作成することを意味しますか? –

+0

それは私が推測するだろう。 –

答えて

1

これはあなたの探しているものですか?

// @flow 

import React from 'react'; 
import type {ComponentType} from 'react'; 

interface StaticInterface { 
    fn(): void; 
} 

class NoStatic extends React.Component<{}> { 
} 

class WithStatic extends React.Component<{}> { 
    static fn() {} 
} 

const c1: ComponentType<{}> = NoStatic;      
const c2: ComponentType<{}> = WithStatic;      
const c3: ComponentType<{}> & StaticInterface = WithStatic; 
// const c4: ComponentType<{}> & StaticInterface = NoStatic;  // NOT OK 

(demo)

私はこれはかなり自分自身を混乱見つけます。

https://blog.bluematador.com/posts/enforcing-method-presence-in-react-components-with-flow

関連:私はこのことから、それを適応

https://github.com/facebook/flow/issues/2048

https://github.com/facebook/flow/pull/3994

+0

ああ、全く私が探していたもの!どうもありがとう :) –

-1

あなたも、正確に知っていますあなたが探している用語。あまりにも多くの先行知識でそれを検索することは簡単です。私の悪い:ちょうどあなたが静的メソッドに

class SampleClass { 
    static test() { 
    console.log('I am a static method') 
    } 
} 
SampleClass.test() 
// 'I am a static method' 

編集を有効にする方法の前にキーワードstaticを使用しています。私はあなたのコードを見ていて、それを解釈しようとしています。私はとても好奇心が強い。 Reactにこのような慣習的でないクラス/コンポーネント構文を使用している理由はありますか?あなたが現在の構文の内容を知っていない限り、自分自身を踏み外しているかもしれません。もっと慣習的な構文で助けを求めることに決めたら、私は使えるかもしれないと思う。

+0

このリンクは質問に答えるかもしれませんが、回答の重要な部分をここに含めて参考にしてください。リンクされたページが変更された場合、リンクのみの回答は無効になります。 - [レビューから](/レビュー/低品質の投稿/ 17445516) – Antti29

+0

これは私の問題ではありません。特定の静的メソッドを公開する 'React.Component'として**引数**を入力したいと思います。私は静的メソッドを宣言することができます(私の例でわかるように) –

+0

@SimonBoudrias Apologies。私は私の質問を更新しました。 – Andrew

関連する問題