2017-01-13 9 views
0

だから、私は相続と対戦の話し合いを全面的に行っていました。私の使用事例についてお話します。リアクションコンポジション対継承

私はreactを使用してコンポーネントの階層を作成しています。ボタン - > buttonBarのようなもの。このようなことをするために、buttonBarコンポーネントにあるように、ボタンコンポーネントで定義した機能が何であれ必要です。

class Button extends React.Component { 
    shouldComponentUpdate() { 
     //do some prevalidation 
     return result; 
    } 

    myButtonfn =() => { 
     //do Something here 
    } 

    myButtonfn2 =() => { 
     //doSomething else 
    } 
} 

class ButtonBar extends Button { 
    shouldComponentUpdate() { 
     return myLogic && super.shouldComponentUpdate.call(this); 
    } 

    myButtonBarfn =() => { 
     //I should be able to do this 
     this.myButtonfn(); 
     this.myButtonfn2(); 
    } 
} 

現在、私はこれを実現するために継承を実装しています。これについて正しい方法は何ですか?私はこれを使用してes5の方法に戻って行きたくありません。 React.createClass。

コンポーネント内の関数の使用方法。

class Button extends React.Component { 
    shouldComponentUpdate() { 
     //do some prevalidation 
     return result; 
    } 

    myButtonfn =() => { 
     //do Something here 
    } 

    myButtonfn2 =() => { 
     //doSomething else 
    } 
} 

class ButtonBar extends React.Component { 
    doSomething =() => { 
     this.refs.abcd.myButtonfn();//To be able to do this, HOC doesn't work 
    } 
    render =() => { 
     return (<Button ref="abcd"/>); 
    } 
} 
+0

高次成分を見てみましょう:このように

小道具として –

+0

@TMitchellちょっと考えて、HOCは彼が何を望んでいるのか少しすぎると思いませんか? –

+0

@TahnikMustasinはどんな意味であまりにも多いですか? –

答えて

0

EDIT: だから、ちょうどまさに私の答えのように見えるあなたの答えを編集しました。これは、現在のそれを使用する方法です。


もしあなたがボタンと呼ばれるクラスを作成することができれば、あなたのbuttonBarはボタンのすべての機能を持つようにボタンを拡張することができます。 - https://facebook.github.io/react/docs/higher-order-components.htmlとあなたの機能を伝承

class Button { 
    constructor(){ 
    //initialise stuff 
    } 

    doFoo(){ 
    //do foo stuff 
    } 
} 

class ButtonBar extends Button{ 
    constructor(){ 
    //initialise stuff 
    } 

    doBar(){ 
    //do bar stuff 
    } 
} 

let myButtonBar = new ButtonBar(); 
myButtonBar.doFoo(); 
+0

このタイプのサンプルは、ES6クラスのように機能しますが、作者のリアクションコンポーネントについてはどうでしょうか。このようなReactコンポーネントを継承することはできません(ReactOOのような第三者のライブラリは必要ありません)。オプションは合成/高次のコンポーネントです –

+0

ええ、 "あなたがes6を使うことができるなら"と言いました。 また、彼の後の編集から、彼は実際にES6を使用しているように見えます。 –

+0

私が「ただのES6クラス」を意味するのは、それらがリアクションコンポーネントではないということです。私はあなたがそれを期待する方法で動作するために、このようにReactコンポーネントを拡張できるとは思わない –

0
class Button extends React.Component { 
    constructor(props, context){ 
     super(props, context); 
     //set local state 
    } 
    shouldComponentUpdate() { 
     //do some prevalidation 
     return result; 
    } 

    const myButtonfn =() => { 
     //do Something here 
    } 

    const myButtonfn2 =() => { 
     //doSomething else 
    } 
} 

class ButtonBar extends Button { 

    constructor(props, context){ 
     super(props, context); 
     //set local state 
    } 
    shouldComponentUpdate() { 
     // No need to bind if you are call super method in ES6 
     return myLogic && super.shouldComponentUpdate(); 
    } 

    myButtonBarfn =() => { 
     //I should be able to do this 
     super.myButtonfn(); 
     super.myButtonfn2(); 
    } 
}