react-native - 我如何将 ref 传递给 state?
问题描述
我创建了一个 ref 并将其作为参数传递给组件。现在我怎样才能在没有调度的情况下将它写入 State?
const myRef = createRef<any>();
const KeyboardAvoidingWrapper: React.FC<IKeyboardAvoidingProps> = (
props: IKeyboardAvoidingProps,
=> {
if (isAndroid) {
return (
<ScrollView ref={myRef} style={styles.scroll} contentContainerStyle={styles.scrollContent}>
<KeyboardAvoiding>{props.children}</KeyboardAvoiding>
</ScrollView>
);
}
解决方案
这个例子可能会对你有所帮助。
<Component ref={(ref) => {
// You can now write ref to state,
// which I will not recommend
// or pass it to callback
props.getInnerRef(ref);
}} />
// You need to pass getInnerRef
<Component getInnerRef={(ref) => {
this.innerRef = ref;
}}
推荐阅读
- python - 将小复杂的正则表达式转换为 python 正则表达式
- c# - Roslyn - CodeFixProvider 正在被解雇,但 DiagnosticAnalyzer 不适用于类析构函数
- c# - 面板控制上的图形创建清除不起作用
- vb.net - 如何放大列表(字符串)(字节)
- anylogic - Anylogic中IF条件的使用
- reactjs - ReactJS:在道具中使用链接而不是标签
- c# - 追加到富文本框中的多个 RTF 文件
- oracle - 如何在oracle中检查最后连接的用户,时间?
- angularjs - angularJS切换按钮如何获取ng-model值
- angular - 如何删除 PrimeNG 编辑器的选项卡功能