Javascript React通过变量为儿童提供多种道具?
需要将一组函数传递给几个子组件。Javascript React通过变量为儿童提供多种道具?,javascript,reactjs,jsx,Javascript,Reactjs,Jsx,需要将一组函数传递给几个子组件。 无论如何,要这样做,请遵循以下原则: render(){ var commonFs = { function1={this.function1}, function2={this.function2}, function3={this.function3}, function4={this.function4}, function4={this.function5},
无论如何,要这样做,请遵循以下原则:
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>
);
}
render(){
var commonFs={
function1={this.function1},
function2={this.function2},
function3={this.function3},
function4={this.function4},
function4={this.function5},
};
返回(
);
}
谢谢 你只需要这样做
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>
);
}
render(){
var commonFs={
函数1:this.function1,
function2:this.function2,
函数3:this.function3,
函数4:this.function4,
函数5:this.function5,
};
返回(
);
}
render(){
var commonFs={
函数1:this.function1,
function2:this.function2,
函数3:this.function3,
函数4:this.function4,
函数4:这个函数
};
返回(
);
}
然后在子级中,您可以使用
this.props.commonFs.function1
等引用公共函数。请记住,将道具传播到组件中会导致代码被滥用,很难理解。语法function1:{this.function1}
不正确。这行不通,谢谢!虽然这会导致functionA | B | C | D
与其他函数不在同一个“范围”中。您能更具体一点吗?{keyName1=value1,keyName2=value2}
是无效语法。啊,是的,您是对的。但是关于commonFs={commonFs}
的部分仍然正确。所以@House3272可以使用这种方法,如果他们愿意的话。
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>
);
}