如何在反应中通过包装 class 公开内部组件的功能?
How do I expose a function of an internal component through a wrapping class in react?
我正在使用@shoutem/ui 库,它有自己的 React Native TextInput 实现。
https://github.com/shoutem/ui/blob/develop/components/TextInput.js
我正在尝试将焦点设置在键盘上的下一个输入上,然后按下按钮,就像这样
但是,Shoutem/ui 的实现没有公开我可以从引用中使用的 TextInput 的 focus() 方法。
如何在外部 class 中将 TextInput 的焦点方法公开为 属性?
import React, { Component } from 'react';
import { TextInput as RNTextInput } from 'react-native';
import { connectStyle } from '@shoutem/theme';
import { connectAnimation } from '@shoutem/animation';
class TextInput extends Component {
focus() {
// how do I expose the react native text input focus method from this class?
// do I get a reference to the text input component somehow?
}
render() {
const { props } = this;
const style = {
...props.style,
};
delete style.placeholderTextColor;
delete style.selectionColor;
return (
<RNTextInput
{...props}
style={style}
placeholderTextColor={props.style.placeholderTextColor}
selectionColor={props.style.selectionColor}
/>
);
}
}
TextInput.propTypes = {
...RNTextInput.propTypes,
style: React.PropTypes.object,
};
const AnimatedTextInput = connectAnimation(TextInput);
const StyledTextInput = connectStyle('shoutem.ui.TextInput')(AnimatedTextInput);
export {
StyledTextInput as TextInput,
};
您可以使用 refs。
focus() {
this.rnInput.focus();
}
render() {
const { props } = this;
const style = {
...props.style,
};
delete style.placeholderTextColor;
delete style.selectionColor;
return (
<RNTextInput
{...props}
ref={input => this.rnInput = input}
style={style}
placeholderTextColor={props.style.placeholderTextColor}
selectionColor={props.style.selectionColor}
/>
);
}
一点解释。 ref 回调在组件完成渲染后执行,然后您将拥有该组件当前实例的引用,您可以将其保存在变量中供以后使用 ref={input => this.rnInput = input}
。现在您可以使用 this.rnInput
调用 focus
方法。
我正在使用@shoutem/ui 库,它有自己的 React Native TextInput 实现。
https://github.com/shoutem/ui/blob/develop/components/TextInput.js
我正在尝试将焦点设置在键盘上的下一个输入上,然后按下按钮,就像这样
但是,Shoutem/ui 的实现没有公开我可以从引用中使用的 TextInput 的 focus() 方法。
如何在外部 class 中将 TextInput 的焦点方法公开为 属性?
import React, { Component } from 'react';
import { TextInput as RNTextInput } from 'react-native';
import { connectStyle } from '@shoutem/theme';
import { connectAnimation } from '@shoutem/animation';
class TextInput extends Component {
focus() {
// how do I expose the react native text input focus method from this class?
// do I get a reference to the text input component somehow?
}
render() {
const { props } = this;
const style = {
...props.style,
};
delete style.placeholderTextColor;
delete style.selectionColor;
return (
<RNTextInput
{...props}
style={style}
placeholderTextColor={props.style.placeholderTextColor}
selectionColor={props.style.selectionColor}
/>
);
}
}
TextInput.propTypes = {
...RNTextInput.propTypes,
style: React.PropTypes.object,
};
const AnimatedTextInput = connectAnimation(TextInput);
const StyledTextInput = connectStyle('shoutem.ui.TextInput')(AnimatedTextInput);
export {
StyledTextInput as TextInput,
};
您可以使用 refs。
focus() {
this.rnInput.focus();
}
render() {
const { props } = this;
const style = {
...props.style,
};
delete style.placeholderTextColor;
delete style.selectionColor;
return (
<RNTextInput
{...props}
ref={input => this.rnInput = input}
style={style}
placeholderTextColor={props.style.placeholderTextColor}
selectionColor={props.style.selectionColor}
/>
);
}
一点解释。 ref 回调在组件完成渲染后执行,然后您将拥有该组件当前实例的引用,您可以将其保存在变量中供以后使用 ref={input => this.rnInput = input}
。现在您可以使用 this.rnInput
调用 focus
方法。