未定义文本输入值
问题描述:
我想从文本输入提交值,但是当我console.log时,之前输入的文本是未定义的。我已经按照react native get TextInput value的说明操作,但仍未定义。未定义文本输入值
这是我做了状态之前:
this.state = {
comment_value: '',
comments: props.comments
}
submit = (args) => {
this.props.submit(args.comment_value)
}
这是对提交的文本的功能:
addComment() {
var comment_value = this.state.comment_value
console.log('success!', comment_value)
})
this.props.submit('410c8d94985511e7b308b870f44877c8', '', 'e18e4e557de511e7b664b870f44877c8')
}
,这是我的TextInput代码:
<TextInput
underlineColorAndroid='transparent'
placeholder='Enter your comment here'
multiline numberOfLines={4}
onChangeText={(text) => this.setState({comment_value: text})}
value={this.state.comment_value}
style={styles.textinput}
/>
</View>
<TouchableOpacity onPress={this.addComment.bind(this)}>
<View style={{flex: 1, flexDirection: 'column', backgroundColor: Colors.background, width: 70}}>
<Icon name='direction' type='entypo' color='#000'size={30} style={styles.icon} />
</View>
</TouchableOpacity>
答
Th是应该肯定是工作的尝试清理你的代码,这样
contructor(props) {
super(props);
this.state = {
comment_value: '',
}
}
addComment() {
console.log(this.state.comment_value); // should be defined
this.props.submit(this.state.comment_value);
}
render() {
return (
<View>
<TextInput
underlineColorAndroid='transparent'
placeholder='Enter your comment here'
multiline numberOfLines={4}
value={this.state.comment_value}
onChangeText={(text) => this.setState({comment_value: text})}
style={styles.textinput}
/>
<TouchableOpacity onPress={this.addComment.bind(this)}>
<View style={{flex: 1, flexDirection: 'column', backgroundColor: Colors.background, width: 70}}>
<Icon name='direction' type='entypo' color='#000'size={30} style={styles.icon} />
</View>
</TouchableOpacity>
</View>
);
}
编辑:基于您的完整的代码示例,好像你错误地试图通过做多this.state = ...
这是不正确的更新状态,更新你的状态必须使用this.setState({ ... })
我写了这样的this.props.submit(“410c8d94985511e7b308b870f44877c8”,“”,“e18e4e557de511e7b664b870f44877c8”),因为我一定要考成功与否发送到服务器 –
的数据,是的,仍然不确定。 –
当你测试你的输入是否显示出来时,你可以对此进行评论,我制作了一个我发布的代码的副本,并且它可以正常工作,你是否可以发布包含该代码的整个页面,因为问题是来自其他地方 – linasmnew