我在调用另一个函数,并都具有相同名称的组件的功能。
它的作品,运行的功能
我想知道为什么它工作(见我的假设下面)? 一个后续问题,如果我想递归调用外部函数我应该使用this.myMethod()
?
此模式是否安全使用?呼叫在道具的方法的函数内使用相同的名称
实施例:
myMethod(){
const {myMethod} = this.props;
if(typeof myMethod == 'function'){ // i was sure this will check the external myMethod func
myMethod(); // i was sure it will call the external myMethod func.
}
// what if i want to call to the external "myMethod" func (recursively)?
}
我假定发动机正在搜索名为myMethod
的外部myMethod
(词法作用域?)体内的功能和它发现它。
任何人都可以证实这个假设?
编辑
只是为了让我的问题如何共同安全的这种模式更加清晰。
比方说,我有Item
一个组成部分,它有一个onTouch
道具PropTypes.func
,我想经营自己的函数里面的一些内部逻辑,然后运行通过props
作为回调或类似的东西收到的功能。
我应该周折才找到一个新的名字,以我的内部功能类似onInternalTouch
或者是它的安全坚持使用相同的名称onTouch
?
onTouch(){ // is this common practice?
const {onTouch} = this.props;
// ... internal component logic
onTouch();
}
onInternalTouch(){ // or should i bother finding a new name?
const {onTouch} = this.props;
// ... internal component logic
onTouch();
}
好吧,你基本上重复了我的逻辑片段。如果我正确理解你,为了调用“父”方法'递归地我需要在它自己内部使用'this.myMethod'(外部'myMethod') –
好吧,刚刚看到你的编辑:) –
这是一种常见的做法吗? (在反应中使用道具) –