2016-04-17 11 views
0

ReactJSおよびES6でコンポーネントクラスを継承しているときに、インスタンス関数を参照するデフォルトプロパティを追加しようとしています。具体的には、私はNPMからの日付ピッカーを持っている(日ピッカー反応する)と2つのプロパティは常に、基本クラスに送信されていることを確認する:ES6でreactjsクラスを継承するときのデフォルトプロパティを追加

export default class DayPicker extends BaseDayPicker { 
constructor(props) { 
    var { ...newProps } = props; 
    newProps.onMouseDown = this.onDayPickerMouseDown; 
    newProps.onMouseUp = this.onDayPickerMouseUp; 
    super(newProps); 
} 

componentDidMount() { 
    super.componentDidMount && super.componentDidMount(); 
    window.addEventListener('mousedown', this.onPageClick, false); 
} 

componentWillUnmount() { 
    super.componentWillUnmount && super.componentWillUnmount(); 
    window.addEventListener('mousedown', this.onPageClick, false); 
} 

onPageClick = (e) => { 
    if (!this.isDayPickerMouseDown) { 
     this.props.onPageClick && this.props.onPageClick(); 
    } 
}; 

onDayPickerMouseDown = (e) => { 
    this.isDayPickerMouseDown = true; 
}; 

onDayPickerMouseUp = (e) => { 
    this.isDayPickerMouseDown = false; 
}; 

render() { 
    return super.render(); 
} 

}

上記コードの問題私は'this' is not allowed before super()を得るということです。

私はこれを解決する方法を見つけることができません。 thisを使用する必要があるデフォルトプロパティを追加できない場合は、レンダリングメソッドで解決できますか?

+0

'super(newProps);'をコンストラクタ関数の一番下に移動します。 – Andy

+0

これは、私の新しいプロパティをレンダリングから除外します。 – Sten

答えて

2

Referencing my comment on another answer

あなたは離れ継承から傾く、それはアンチパターンである必要があります。

反応がの組成のために設計された。 それはどういう意味ですか?共有する機能がある場合は、それをコンポーネントに入れ、さまざまな方法で小道具を使用させます。

TL; DRあなたはこのような状況のため高次コンポーネントを使用します。

例:あなたは、理由を知りたい場合は

BaseDayPicker = (RenderedComponent) => React.Component { 
    // just a function that takes a component, and returns a component. 
    onMouseDown() { 
    this.props.onMouseDown(doSomething()); 
    } 

    onMouseUp() { 
    this.props.onMouseUp(); 
    } 

    //... 
    // notice it renders the component that came in as a parameter. 
    render(){ 
    return (<RenderedComponent 
     onMouseUp={this.onMouseUp} 
     onMouseDown={this.onMouseDown} 
    />) // it also adds some props! super cool 
    } 
} 

class DayPicker extends React.Comnponent { 
    //... 

    onMouseDown() { 
    this.isDayPickerMouseDown = true; 
    this.props.onMouseDown(); 
    } 

    onMouseUp() { 
    this.isDayPickerMouseDown = false; 
    this.props..onMouseUp(); 
    } 

    //.... 
} 
// NOTICE, WRAPPING ONE IN ANOTHER 
export BaseDayPicker(DayPicker) 

ここreact mixins are dead理由を説明するブログ記事です。

+0

ブログ記事をより詳しく説明しています。https://medium.com/@blairanderson/how-to-create-react-higher-order-components-and-why-do-i-care-210a4ad48061#.243ym7uec –

0

可能な解決策の1つは、親クラスで2つの空の関数を指定し、子クラスでそれらをオーバーライドすることです。

class BaseDayPicker extends React.Comnponent { 
    //... 

    onMouseDown() { 
    this.props.onMouseDown(); 
    } 

    onMouseUp() { 
    this.props.onMouseUp(); 
    } 

    //.... 
} 

class DayPicker extends React.Comnponent { 
    //... 

    onMouseDown() { 
    this.isDayPickerMouseDown = true; 
    super.onMouseDown(); 
    } 

    onMouseUp() { 
    this.isDayPickerMouseDown = false; 
    super.onMouseUp(); 
    } 

    //.... 
} 

この場合、クラスメソッドと関数をprops内で渡すことができます。

BaseDayPickerを、コンポーネント自体を拡張しようとする代わりに機能を拡張する別のコンポーネントにラップする方がよい場合があります。

+0

基本クラスのコードを変更したくないと言わざるを得ないでしょう(npmモジュールへのプルリクエストを行うのがベストソリューションです。 – Sten

+0

BaseDayPickerクラスのソースコードを表示できますか? –

+0

これはこれです:https://github.com/gpbl/react-day-picker(これは私が書いたものではありません)。私はコードをBaseDayPickerと呼んでいるので、私は の "default-BaseDayPicker"を "react-day-picker"からインポートします。 – Sten

関連する問題