TextInput onChange事件接收的是文本对象而不是字符串
TextInput onChange event receives text object not string
我正在尝试从ListView组件中的TextInput onChange
事件更新redux状态。在我的例子中,我显示了一个比率列表。我想将编辑后的比率解析为两个值。然而,传递到setItem
方法中的text
值并不是我所期望的字符串,而是一个Text或TextInput对象。
editItem
是对冗余状态进行编辑的冗余动作。
如何获取输入字符串值,以便将其传递给操作?
class DataList extends Component {
constructor(props) {
super(props);
// Bind the function so we can use it on onPress event
this.renderRow = this.renderRow.bind(this);
const { data } = this.props.currentData;
this.dataSource = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2
})
}
render() {
// Keep dataSource up to date with redux state
const { data } = this.props.currentData;
const dataSource = this.dataSource.cloneWithRows(data.list);
return (
<View>
<ListView
dataSource={dataSource}
renderRow={this.renderRow}
renderSeparator={(sectionID, rowID) => <View key={`${sectionID}-${rowID}`} style={styles.separator} />}
style={[styles.list, {height: windowDims.height}]}
initialListSize={1}
/>
</View>
)
}
setItem(index, text) {
console.log (index + "; [" + text + "]")
console.log (text) // Prints out what looks like a Text or TextInput object
var { data } = this.props.currentData;
var numerator = parseInt (text.substring (0, text.indexOf('/'))) // Fails as it does not find a string
var denominator = parseInt (text.substring (text.indexOf('/')+1))
this.props.dispatch(editItem(index, { numerator: numerator, denominator: denominator }));
}
renderRow(rowData, sectionID, rowID) {
return (
<View style={styles.rowContainer}>
<TextInput
style={styles.textInput}
keyboardType={'numeric'}
onChange={(text) => this.setItem(rowID, text)}
value={rowData.numerator + "/" + rowData.denominator}
/>
</View>
);
}
}
在TextInput文档中,我认为您需要使用onChangeText事件。
更新:使用onChangeText将为您提供一个对象,该对象包含一个具有更改后的文本字符串的文本属性。
相关文章:
- jQuery匹配JSON对象的部分文本
- JavaScript正则表达式文本与RegExp对象
- 文本表示法VS.构造函数,用于在JavaScript中创建对象
- 嵌套对象文本访问父级
- .notion vs.[]notation |插入到文本对象中
- 在node.js中使用JavaScript从JSON对象获取和设置文本内容
- 如何从jQuery对象文本正确调用服务器端ASP.NET并将数据返回给jQuery
- 在SVG中创建带有外来对象的文本区域的Javascript不起作用
- 使用javascript函数检索隐藏的文本值时出现对象HTMLinputElement错误
- TextInput onChange事件接收的是文本对象而不是字符串
- 使用结构 js 向右/向左倾斜画布文本对象
- 文本对象到路径:如何从使用Cambam字体的Raphael js中使用打印方法时生成的双路径中删除单个路径
- 将其绑定到嵌套文本对象中
- jQuery在单击不同的输入文本对象时替换文本
- 当将多个子元素附加到svg文本对象时,每次都需要一个新的子元素
- 如何在结构文本对象中应用自定义字体
- Javascript文本对象符号This vs Object Name
- 如何在PIXI.js的本地容器中居中文本对象
- 文本对象中的HTML
- 为函数中的文本对象设置样式