reactjs - 如何在本机反应中获取文本输入的文本?
问题描述
如何用 React 语法编写这个语句,就像在 ios 中一样:
txt1.text = label.text
我尝试了很多但失败了。
import React, { Component } from 'react';
import { View, Text, Button, TextInput } from 'react-native';
export default class App extends Component {
state = {
TextInputValue: '',
};
onPress = () => {
this.setState({
//textValue.Text: TextInputValue,
//newText: textValue
});
};
render() {
return (
<View style={{ paddingTop: 25 }}>
<TextInput
style={{ height: 40 }}
placeholder="Type here to! Translate"
onChangeText={TextInputValue => this.setState({ TextInputValue })}
/>
<Button title="Change Text" onPress={this.onPress}
/>
<Text>
{this.state.newText}
</Text>
</View>
);
}
}
按下按钮时,我想获取文本textinput
并将其显示在文本(标签)上
解决方案
两个缺失的元素:
newText
在状态结构中定义。
state = {
TextInputValue: '', // for onChangeText handler
newText: '', // for button onClick handler
};
- 实现按钮
onClick
处理程序以设置newText
状态。
onPress = () => {
this.setState({
newText: this.state.TextInputValue,
});
}
请参阅代码沙盒演示
推荐阅读
- python - For循环条件如何打印值直到索引5
- javascript - 为什么这个二分搜索找不到我的值?
- python - Selenium Python 无法提取 [object Text] 它应该是一个元素
- javascript - 将日期转换为 M j, Y?在 Javascript 中
- vue.js - 我想要做的是使用单个打印按钮从多台打印机同时打印表单的不同打印输出
- regex - 带有组名并包含标签的正则表达式 XML
- sql - 基于字符串模式检索父记录的 Oracle SQL 查询
- cryptography - 密码学:关于流密码的问题
- python - 如何为循环的特定部分添加延迟
- javascript - 将组件作为 React setState 值传递时有问题吗?