React包含多个道具通过变量为孩子?

时间:2017-08-18 23:37:15

标签: javascript reactjs jsx

需要将一组函数传递给几个子组件 无论如何要这样做:

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>
    );
}

谢谢!

2 个答案:

答案 0 :(得分:1)

你只需要这样做

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 :(得分:0)

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>
    );
}

然后在孩子内部,您可以使用this.props.commonFs.function1等参考常见功能......