【发布时间】:2017-05-11 14:07:46
【问题描述】:
我在 React Native 应用程序中使用 Redux Form (RF)。一切正常,但我不知道如何从 Field 输入中获取 refs 以使用 Redux Form 转到下一个输入字段。
没有 RF this 解决方案可以正常工作。
这是我的代码:
class RenderInput extends Component {
const { input, nextField, refs,
meta: { touched, error, warning },
input: { onChange } } = this.props
render() {
return (
<Input
returnKeyType = {'next'}
onChangeText={onChange}
onBlur={input.onBlur}
onFocus={input.onFocus}
onSubmitEditing = {(event) => {
// refs is undefined
refs[nextField].focus()
}}/>
)
}
}
class Form extends Component {
render() {
return (
<Field
name="field1"
focus
withRef
ref='field1'
nextField = "field2"
component={RenderInput}/>
<Field
name="vendor"
withRef
ref="field2"
nextAction = "field3"
component={RenderInput}/>
)
}
}
当单击键盘上的Next 键时,我将属性nextField 传递给组件以确定下一个输入字段,但我无法在RenderInput 组件中获取refs 属性。
知道如何获取 refs 属性吗?
【问题讨论】:
-
只是说,直接分配 ref 已被弃用。最好的办法是将 ref 分配给一个可以重用的全局变量:
ref={(componentRef) => this.myRef = componentRef}。见React Documentation -
我在这里发帖 stackoverflow.com/a/56401396/6242582 我是如何解决这个问题的。