react-native - 无法在 react-native 中调用父方法
问题描述
我需要调用方法 nameList()。它是父方法。但这对我不起作用。我的代码片段如下。
以下是我的代码片段参考,任何答案都应该受到赞赏。我需要对此进行澄清
nameList = refName => () => {
if(!nameList){
get call
}
else(){
update call
}
}
renderNamesRight(selectedName) {
if (selectedNames.length > 0 ) {
return(
this.renderCenterNames()
)
}
}
renderCenterNames(){
if (!this.state.pressedCenterNames) {
return(
<TouchableOpacity onPress = {()=> this.validateInputFields()} activeOpacity = { .5 }>
<Text style = {styles.names}>CENTER NAMES</Text>
</TouchableOpacity>
)
} else {
return(
<Text style = {styles.names}>RIGHT NAMES</Text>
)
}
}
validateInputFields() {
const { validation} = this.state;
if (this.state.name) {
alert("validation")
validation.isFormValid = true;
this.setState({validation: validation});
this.nameList("name")
} else {
validation.isFormValid = false;
this.setState({validation: validation});
}
}
解决方案
这不是您定义函数的方式。如果你需要一个没有参数的函数,你必须像这样定义FunctionName = () => {}
或使用参数FunctionName = (parameters) => {}
。
如果函数中只有一个参数,您也可以 像这样在没有()的情况下声明它FunctionName = parameter => {}
替换您的方法定义
nameList = refName => () => {
有了这个
nameList = (refName) => {
推荐阅读
- r - 在 R Shiny 中创建一个带有复选框的网格
- javascript - Ajax 中的 if/else 语句简写
- android - 奇怪的问题-Redmi note4-无法通过adb识别
- list - AttributeError:“列表”对象没有属性“值”
- android - 清单中的 Unity 2018.2 构建错误(资源 drawable/app_icon 未找到)
- html - 如何覆盖特定页面中的 CSS 重要规则
- javascript - 如何对具有相同键的对象值求和并返回一个对象?
- r - 按组嵌套数据框,但在每个组中包含额外的行
- docker - 从两个不同的容器映射端口
- system-verilog - 有没有办法在测试结束时获得断言触发(失败)的次数