如何在Javascript中调用动态命名的方法。
我正在使用React Native
,并且在ref
中分配TextInput
时,需要动态设置该方法。
renderInput(id) {
<TextInput
ref={(input) => this.myInput = input}
/>
}
我需要动态this.myInput
。
我已经测试过并弄错了:this.myInput[id]
和this[id].myInput
答案 0 :(得分:2)
您应该使用current:
this.myInput.current[id]
方法如下:
// First, create a reference (in the constructor)
this.myInput = React.createRef()
// Then, assign the ref (in the element)
<TextInput ref={this.myInput} />
// Now, to access the reference
// this.myInput.current.<<property you wanted to access>>
this.myInput.current[id] // id is defined as a variable
// this will get the dom id
// this.myInput.current.id
但是,如果您坚持使用当前使用的回调ref,则可以传递id属性:(此外,我认为您正在寻找的答案是)
renderInput(id) {
<TextInput
ref={(input) => this.myInput = input}
id={id} {/* you were missing this in fact */}
/>
}
现在,要获取该ID:
this.myInput.id // will work fine, coz we have id prop now
// gets DOM id that you assigned in your element
答案 1 :(得分:1)
reg <- systemfit::systemfit(Equation[[1]], method = "3SLS", inst = Inststruments[[1]],
methodResidCov = "noDfCor", data = DF[[1]])
> Error in intI(i, n = x@Dim[1], dn[[1]], give.dn = FALSE) :
index larger than maximal 0
//使用引用
constructor(props) {
super(props);
this.inputs = {};
}
// params Object should be in this format {id:324, referenceKey: 'one'};
renderTextInput(params) {
return <TextInput ref={input => { this.inputs[params.referenceKey] = input }}/>
}