React-Native:ScrollView,refs和自定义组件
React-Native: ScrollView, refs, and custom components
我试图了解如何在 React-Native 中做 2 件事。
问题 1:我有一个组件和另一个自定义组件,我需要在 ScrollView 中滚动。
问题是我太新了,无法理解如何从我的"子"组件访问滚动视图。注意:如果我不使用自定义组件并将所有内容放入 MainView 组件中,则代码工作正常。
以下是代码的基础知识:
class MainView extends React.Component{
render() {
return (
<ScrollView ref='scrollView'>
<MyCustomComponent />
</ScrollView>
)
}
}
class MyCustomComponent extends React.Component{
inputFocused(refName) {
setTimeout(() => {
let scrollResponder = this.refs.scrollView.getScrollResponder();
scrollResponder.scrollResponderScrollNativeHandleToKeyboard(
React.findNodeHandle(this.refs[refName]),
110, //additionalOffset
true
);
}, 50);
}
render() {
return (
<TextInput ref='fieldtitle'
onFocus={this.inputFocused.bind(this, 'fieldtitle')}
/>
)
}
}
问题 2:如何让我的"子"(MyCustomComponent)组件调用在父组件 (MainView) 上实现的方法。这样做的原因是我有一堆略有不同的"MyCustomComponents",我不想实现相同的样板代码来滚动所有这些组件。
对于问题 2:我正在使用的方法是将该方法传递到子组件的 props 中,并在需要时调用它。
感谢@yang-lei为我指出一个方向。这是我最终做的事情:
对于"父"组件中的"子"组件,在函数中传递:
<MyCustomComponent onFocusFunction={this.gainFocus.bind(this)} onDismissFunction={this.dismissFocus.bind(this)} />
在 MyCustomComponent 中:
inputFocused(refName) {
this.props.onFocusFunction(this, refName)
}
dismissFocus(refName) {
this.props.onDismissFunction(this, refName)
}
render() {
var field = this.props.fieldInfo;
placeholder = field.placeholder ? field.placeholder : ""
return (
<View style={styles.inputRow}>
<Text style={styles.textField}>{this.props.fieldInfo.title}</Text>
<TextInput ref={field.title}
style={styles.textInput}
onChange={this.handleChange.bind(this)}
keyboardType='email-address'
placeholder={placeholder}
onFocus={this.inputFocused.bind(this, field.title)}
onBlur={this.dismissFocus.bind(this, field.title)}
/>
</View>
)
}
在"父"组件中:
gainFocus(view, refName) {
setTimeout(() => {
let scrollResponder = this.refs.scrollView.getScrollResponder();
scrollResponder.scrollResponderScrollNativeHandleToKeyboard(
React.findNodeHandle(view.refs[refName]),
110, //additionalOffset
true
);
}, 0);
}
dismissFocus(view, refName) {
setTimeout(() => {
let scrollResponder = this.refs.scrollView.getScrollResponder();
scrollResponder.scrollResponderScrollTo(
)
}, 0);
}
相关文章:
- 对自定义组件中的本地访问引用进行反应
- 点击tpl链接时,ExtJS 4.2自定义组件触发事件
- Extjs 4,事件处理,范围,自定义组件
- Gwd自定义组件及其事件处理程序
- 从自定义组件内部查找 Ember
- 在 ionic 2 中缓存自定义组件内容
- 挖空绑定自定义组件,使其不与中心视图模型冲突
- 使用 Aurelia在视图中动态生成自定义组件
- Ember.js自定义组件或视图
- Vue.js 使用 Vuex 和自定义组件,@click方法输出未定义
- 如何侦听 RactiveJS 自定义组件
- VideoJS v5 - 在 ES6 中添加自定义组件 - 我做得对吗?
- 如何将自定义组件与反应路由器路由转换一起使用
- React-Native:ScrollView,refs和自定义组件
- extjs4 - 将自定义组件添加到容器
- JSF 自定义组件在与 Oracle ADF 一起使用时缺少自动生成的 JavaScript
- 嵌入 API 自定义组件:未捕获类型错误:无法读取未定义的属性“ViewSelector2”
- KnockoutJS:访问自定义组件之外的可观察变量
- 控制铁页的聚合物自定义组件
- Sencha touch 2-自定义组件开发数据项未从存储加载值