Javascript React native:动态调用方法

Javascript React native:动态调用方法,javascript,reactjs,react-native,Javascript,Reactjs,React Native,如何在Javascript中调用动态命名方法 我正在使用React Native,在TextInput中分配ref时,我需要动态设置方法 renderInput(id) { <TextInput ref={(input) => this.myInput = input} /> } renderInput(id){ this.myInput=input} /> } 我需要动态这个.myInput 我已经测试过了,但结果是错误的:this.myI

如何在Javascript中调用动态命名方法

我正在使用
React Native
,在
TextInput
中分配
ref
时,我需要动态设置方法

renderInput(id) {
    <TextInput
        ref={(input) => this.myInput = input}
    />
}
renderInput(id){
this.myInput=input}
/>
}
我需要动态
这个.myInput

我已经测试过了,但结果是错误的:
this.myInput[id]
this[id]。myInput
您应该使用:


以下是方法:

// 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

不太清楚你的意思。什么是“dynamic this.myinput”?你应该读一下Mahdi…他在组件中是否正确分配了他的ref
this.myInput=React.createRef(),否则他将无法访问它。@KurtMiller在你评论时正在更新。确定。我认为这是非常基本的。OP似乎没有太多的研究。。。。。例如,我会帮他更新答案。据我所知,OP缺少id道具。谢谢。但我做了,而且弄错了无法将属性“one”设置为“undefined”。您需要使用名称input.constructor(){this.input:{};}定义状态。我这样做了,但没有得到任何结果无需定义为状态变量。只需定义为this.inputs={}就足够了
// 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
renderInput(id) {
    <TextInput
        ref={(input) => this.myInput = input}
        id={id} {/* you were missing this in fact */}
    />
}
this.myInput.id // will work fine, coz we have id prop now
// gets DOM id that you assigned in your element
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 }}/>
} 
componentDidMount(){
  this.inputs['one'].focus()
}