Javascript 在react native中使用HOC时,传递一个TextInput,此TextInput无法聚焦
我正在尝试传递一个组件并返回一个新组件, 它可以工作,但当传递TextInput时,我发现TextInput无法聚焦 我的react本机版本是0.53.3 输入一个字符后,它就模糊了 当我在react native 0.47.2上运行代码时 它甚至无法集中注意力 我用错了吗 这是我的HOC函数Javascript 在react native中使用HOC时,传递一个TextInput,此TextInput无法聚焦,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我正在尝试传递一个组件并返回一个新组件, 它可以工作,但当传递TextInput时,我发现TextInput无法聚焦 我的react本机版本是0.53.3 输入一个字符后,它就模糊了 当我在react native 0.47.2上运行代码时 它甚至无法集中注意力 我用错了吗 这是我的HOC函数 const addTitle = Comp => class extends React.PureComponent { render() { const { titl
const addTitle = Comp =>
class extends React.PureComponent {
render() {
const { title, ...childProps } = this.props;
return (
<View style={{ paddingHorizontal: 20 }}>
<View style={{ flexDirection: 'row', marginVertical: 5 }}>
<Text style={styles.subText}>{title}</Text>
<Text style={[styles.subText, { color: 'red' }]}>{'*'}</Text>
</View>
<Comp {...childProps} />
</View>
);
}
};
只需将const Enganced=addTitle(TextInput)移到renderTitle方法之外。像
const engansed=addTitle(TextInput);
类SomeComponent扩展了React.Component{
//代码
}
您可以尝试将const Enganced=addTitle(TextInput)
移动到类外(到文件全局命名空间)吗?是的,问题解决了,谢谢much@rick.tongxue,我已经回答了。请,除了这个答案,其他人会知道问题已经解决。请你简要描述一下为什么会发生这种情况?@skyboyer我已经更新了答案,希望可以理解。
renderTitle = () => {
const { title } = this.state;
const Enhanced = addTitle(TextInput);
return (
<Enhanced
autoCorrect={false}
autoCapitalize={'none'}
style={styles.textInput}
title={'title'}
value={title}
onChangeText={this._onChangeText('title')}
/>
);
};
_onChangeText = attribute => value => {
this.setState({
[attribute]: value,
});
};