react-native - 滚动到 react-native scrollview 动态子项
问题描述
我创建了一个呈现动态表单元素的滚动视图。我可以为滚动视图的每个动态子项创建“ref”。但我面临的问题是我想滚动到表单中存在验证错误的确切子项。但是我找不到每个孩子相对于滚动视图的实际位置,因此我可以滚动到该元素。
我曾尝试将 onLayout 方法添加到子组件,但它在加载时只返回一次位置,然后该值保持不变,即使在滚动之后也是如此。
我的滚动视图
<ScrollView style={{marginBottom: 55}} ref={(ref) => { this.formScrollList = ref; }}>
{this.state.formElements.map((element, i) => {
return (
<TextboxInput quesNo={element.key + 1} required={element.required} error={this.state.errors[element.question_id]} quesType={element.type} quesId={element.question_id} placeholder={element.placeholder} onChangeHandler={this.onChangeHandler} label={element.label} defaultValue={this.state.answers[element.question_id]} showRequired={this.showRequired} index={element.index}/>
)
})}
</ScrollView>
我的 TextboxInput 孩子
const TextboxInput = (props) => (
<View style={styles.inputContainer} ref={setRef}>
<Text style={styles.text}>{props.label || "Textbox"}{props.showRequired(props.required)}</Text>
<TextInput style={[styles.textInput, props.error === "empty" ? {borderWidth: 1, borderColor: "red"} : null]} {...props} onChangeText={props.onChangeHandler.bind(this, props.quesId, props.quesType, props.index)} placeholder={props.placeholder} underlineColorAndroid="transparent" defaultValue={props.defaultValue}/>
</View>
)
我想滚动到表单中存在验证错误的确切子项。
解决方案
使用 scrollview 组件的 scrollTo 方法。
推荐阅读
- bash - 如何遍历子目录中的文件?
- ios - 使用硬件键盘 iOS Swift 按“Shift”时 WKWebView 应用程序崩溃
- java - 当我尝试从 ManyToOne 关系中获取对象时出现 LazyLoadingException
- react-native - 无法从函数外部调用新选择的日期/时间数据
- opencv - Aws Sagemaker - ModuleNotFoundError:没有名为“cv2”的模块
- jsf - datagrid Primefaces中的分页
- fastapi - 验证 OAuth2PasswordRequestForm
- octave - 如何在 Octave 中设置环境小数点分隔符?
- c# - 为长时间运行的进程 .Net Core 实施 HealthChecks
- javascript - (React Native):任务':app:generatePackageList'执行失败