React native 反应本机复选框不工作
我有一个由表单组成的react原生应用程序。我已经实现了两个复选框,它们的状态设置为布尔真。我希望布尔值true或false从这些复选框提交到数据库,但复选框按钮不起作用。 这是我的密码:React native 反应本机复选框不工作,react-native,checkbox,axios,React Native,Checkbox,Axios,我有一个由表单组成的react原生应用程序。我已经实现了两个复选框,它们的状态设置为布尔真。我希望布尔值true或false从这些复选框提交到数据库,但复选框按钮不起作用。 这是我的密码: import React, { Component } from 'react'; import { View, Text, Button, TextInput, StyleSheet } from 'react-native'; import { CheckBox } from 'react-native-
import React, { Component } from 'react';
import { View, Text, Button, TextInput, StyleSheet } from 'react-native';
import { CheckBox } from 'react-native-elements';
import axios from 'axios';
export default class Delivery extends Component {
constructor(props) {
super(props);
this.state = {
trackingNo: '',
weight: '',
length: '',
width: '',
//checked: true,
delivered: { checked: true }
};
}
componentDidMount(){
fetch('https://courierapp-test.herokuapp.com/products/')
.then(
function(response) {
if (response.status !== 200) {
console.log('Looks like there was a problem. Status Code: ' +
response.status);
return;
}
// Examine the text in the response
response.json().then(function(data) {
console.log(data);
});
}
)
.catch(function(err) {
console.log('Fetch Error :-S', err);
});
}
onPressSubmit(){
axios.post('https://courierapp-test.herokuapp.com/products/', {
requestId: this.state.trackingNo,
parcelWeight: this.state.weight,
parcelLength: this.state.length,
parcelWidth: this.state.width,
parcelPickup: this.state.pickup,
parcelDelivered: this.state.delivered,
})
.then(function (response) {
console.log(response, "data sent");
})
.catch(function (error) {
console.log(error, "data not sent");
});
}
render() {
return (
<View style={{padding: 15}}>
<TextInput
style={styles.inputStyle}
placeholder="Request Id"
onChangeText={(trackingNo) => this.setState({trackingNo})}
value={this.state.trackingNo}
/>
<CheckBox style={styles.checkStyle}
title='Pickup'
checked={this.state.checked}
/>
<CheckBox style={styles.checkStyle}
title='Delivered'
onValueChange={() => this.setState({ checked: !this.state.checked })}
/>
<TextInput
style={styles.inputStyle}
placeholder="Enter Parcel Weight(Kg)"
onChangeText={(weight) => this.setState({weight})}
value={this.state.weight}
/>
<TextInput
style={styles.inputStyle}
placeholder="Enter Parcel Length(Cm)"
onChangeText={(length) => this.setState({length})}
value={this.state.length}
/>
<TextInput
style={styles.inputStyle}
placeholder="Enter Parcel Width(Cm)"
onChangeText={(width) => this.setState({width})}
value={this.state.width}
/>
<Button
onPress={() => {
this.onPressSubmit()
}
}
title="Submit"
color="#1ABC9C"
/>
</View>
);
}
}
const styles = StyleSheet.create({
inputStyle: {
color: '#1ABC9C',
height: 40,
borderWidth: 0
}
});
import React,{Component}来自'React';
从“react native”导入{视图、文本、按钮、文本输入、样式表};
从“react native elements”导入{CheckBox};
从“axios”导入axios;
导出默认类传递扩展组件{
建造师(道具){
超级(道具);
this.state={
跟踪号:“”,
重量:'',
长度:'',
宽度:“”,
//核对:对,
已传递:{已检查:真}
};
}
componentDidMount(){
取('https://courierapp-test.herokuapp.com/products/')
.那么(
功能(响应){
如果(response.status!==200){
console.log('看起来有问题。状态代码:'+
答复(现状);
返回;
}
//检查回复中的文本
response.json().then(函数(数据){
控制台日志(数据);
});
}
)
.catch(函数(err){
log('Fetch Error:-S',err);
});
}
onPressSubmit(){
轴心柱https://courierapp-test.herokuapp.com/products/', {
requestId:this.state.trackingNo,
包裹重量:this.state.weight,
包裹长度:这个.state.length,
地块宽度:this.state.width,
皮卡:这个州皮卡,
包裹交付:本州已交付,
})
.然后(功能(响应){
控制台日志(响应,“发送数据”);
})
.catch(函数(错误){
日志(错误,“数据未发送”);
});
}
render(){
返回(
this.setState({trackingNo})
值={this.state.trackingNo}
/>
this.setState({checked:!this.state.checked})}
/>
this.setState({weight})}
值={this.state.weight}
/>
this.setState({length})}
值={this.state.length}
/>
this.setState({width})
值={this.state.width}
/>
{
这是onPressSubmit()
}
}
title=“提交”
color=“#1ABC9C”
/>
);
}
}
const styles=StyleSheet.create({
输入样式:{
颜色:“#1ABC9C”,
身高:40,
边框宽度:0
}
});
我试过了所有的方法,但都解决不了。该复选框仅在单击时持续闪烁,但无法更改选中或未选中的状态,也无法将任何值提交到数据库。定义处于选中状态的变量
this.state = {
checked: false
};
创建一个方法
onChangeCheck() {
this.setState({ checked: !this.state.checked})
}
并使用Chekbox的onChange()
prop
更新状态并向复选框提供值,如下所示
<CheckBox
style={styles.checkBox}
value={this.state.checked}
onChange={() => this.onChangeCheck()} />
this.onChangeCheck()}/>
尝试更改事件内部的值onValueChange
<CheckBox value={this.aBooleanField} onValueChange={() => {
this.aBooleanField = !this.aBooleanField;
this.updateView();
}}/>
也适用于 组件,您可以使用updateView仅刷新视图,设置状态以保持状态值(当您恢复应用程序、旋转屏幕等时)。您尝试使用this.state.checked
但它不是this.state.delivered.checked
?@Li357抱歉输入错误。我甚至尝试过删除它,但仍然不起作用。好的答案很重要,要记住内联onCheck或onClick事件在页面上被阻止,您需要将它们移动到一个称为onChange/onClick事件的函数中。
updateView() {
this.setState({ viewChanged: !this.state.viewChanged})
}