javascript - React Native 方法中的参数
问题描述
这是我试图理解的示例代码。
class Form extends React.Component {
constructor (props) {
super(props)
this.state = {
input: ''
}
}
handleChangeInput = (text) => {
this.setState({ input: text })
}
render () {
const { input } = this.state
return (
<View>
<TextInput style={{height: 40, borderColor: 'gray', borderWidth: 1}}
onChangeText={this.handleChangeInput}
value={input}
/>
</View>
)
}
}
在这个困扰我的代码是
onChangeText={this.handleChangeInput}
在这段代码中,没有参数传递给需要文本的方法(handleChangeInput)。
有人可以在这里放灯吗。
解决方案
基本上,您传递的是对具有相同签名的函数的引用。
React 函数参考可能有助于阐明一些问题。
给定
callback = param => { /* do stuff */ };
然后
componentFn={callback};
相当于
componentFn = param => { /* do stuff */ };
但
componentFn={param => callback(param)}
相当于
component = param => { callback(param) } // call function that does stuff
直接引用只是删除了中间函数绑定。
推荐阅读
- python - 从颜色直方图创建 CSV 数据表
- powerbi - 在分页报告中将“ALL”设置为默认值
- python - 有没有办法改进找到非常大函数的子字符串的函数
- r - 通过 RMarkdown 文档传递参数?
- rust - 弱引用其所有者的结构的构造函数
- c# - 如何使用从视图到视图模型的绑定重构代码
- snowflake-cloud-data-platform - 无法解析具有多个值的 json 字段,以便使用 sql 匹配值
- python - 我可以使用卷积滤波器而不是密集层进行分类吗?
- r - 使用 qtm() 和 openstreetmaps 在 do.call(rbind, x) 中出错
- java - 如何获取 concatWith 中使用的第一个 Flowable 的大小