【发布时间】:2021-08-19 09:39:04
【问题描述】:
我正在尝试实现 TextInput,但当我需要从中获取文本时,我陷入了困境。 我从其他 StackOverflow 问题中尝试了很多选项,但对我没有任何帮助。
这是我使用 TextInput 的代码:
export default class HomeScreen extends Component {
constructor(props) {
super(props)
this.state = {
text: ''
};
}
show = () => {
console.log(this.state.text)
}
render() {
return (
<View>
<View style={{backgroundColor: 'purple', height: '100%'}}>
<View>
<Button title={'test'} onPress={this.show}></Button>
<MyTextInput
btn={1}
placeholder={'test'}
isDateTime={false}
onChangeText={(value) => this.setState({text: value})
value={this.state.text} />
</View>
</View>
</View>
)
}
这里是 MyTextInput 代码:
export function MyTextInput(props) {
const [show, setShow] = useState(false);
const btn = props.btn;
const inputType = () => {
if(props.isDateTime) {
setShow(true);
}
else {
setShow(false);
}
}
return (
<View style={btn ? styles.container : styles.container2}>
<TextInput
style={btn ? styles.input : styles.input2}
{...this.props}
placeholder={props.placeholder}
onFocus={inputType}
showSoftInputOnFocus={props.isDateTime ? false : true} />
{show && (
<DTPicker />
)}
</View>
);
}
当我点击按钮时,我得到了这个:
[Info] 06-01 07:24:59.158 6962 7031 I ReactNativeJS:
我的错误在哪里或者我应该做些什么不同的事情?
【问题讨论】:
-
这能回答你的问题吗? react native get TextInput value
-
不幸的是,没有。我从那里尝试了一些解决方案,但对我没有任何效果。
标签: react-native use-state react-native-textinput