Perlu mewariskan satu set fungsi ke beberapa komponen turunan.
Pokoknya untuk melakukannya di sepanjang baris ini:

render(){

    var commonFs = {
        function1={this.function1},
        function2={this.function2},
        function3={this.function3},
        function4={this.function4},
        function4={this.function5},
    };

    return(
        <div>
            <ChildA {commonFs} functionA={this.functionA} />
            <ChildB {commonFs} functionB={this.functionB} />
            <ChildC {commonFs} functionC={this.functionC} />
            <ChildD {commonFs} functionD={this.functionD} />
        </div>
    );
}

Terima kasih!

0
House3272 19 Agustus 2017, 02:37

2 jawaban

Jawaban Terbaik

Anda hanya perlu melakukan ini

render(){

    var commonFs = {
        function1: this.function1,
        function2: this.function2,
        function3: this.function3,
        function4: this.function4,
        function5: this.function5,
    };

    return(
        <div>
            <ChildA {...commonFs} functionA={this.functionA} />
            <ChildB {...commonFs} functionB={this.functionB} />
            <ChildC {...commonFs} functionC={this.functionC} />
            <ChildD {...commonFs} functionD={this.functionD} />
        </div>
    );
}
1
philraj 19 Agustus 2017, 19:00
render(){

    var commonFs = {
        function1: this.function1,
        function2: this.function2,
        function3: this.function3,
        function4: this.function4,
        function4: this.function5
    };

    return(
        <div>
            <ChildA commonFs={commonFs} functionA={this.functionA} />
            <ChildB commonFs={commonFs} functionB={this.functionB} />
            <ChildC commonFs={commonFs} functionC={this.functionC} />
            <ChildD commonFs={commonFs} functionD={this.functionD} />
        </div>
    );
}

Kemudian di dalam anak, Anda dapat mereferensikan fungsi umum dengan this.props.commonFs.function1 dll...

0
aherriot 19 Agustus 2017, 17:38