gpt4 book ai didi

react-native - react native 复选框不起作用

转载 作者:行者123 更新时间:2023-12-03 21:21:36 28 4
gpt4 key购买 nike

我有一个由表单组成的 native react 应用程序。我已经实现了两个复选框,它们的状态设置为 bool 值 true。我希望 bool 值 true 或 false 从这些复选框提交到数据库,但复选框按钮不起作用。这是我的代码:

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
}
});

我已经尝试了所有解决方法,但无法解决。复选框仅在单击时保持闪烁,但无法更改选中或未选中状态,也没有值提交到数据库。

最佳答案

在状态中定义变量checked

 this.state = {
checked: false
};

创建一个方法

 onChangeCheck() {
this.setState({ checked: !this.state.checked})
}

并使用 ChekboxonChange() 属性来更新状态并像这样向复选框提供 value

<CheckBox
style={styles.checkBox}
value={this.state.checked}
onChange={() => this.onChangeCheck()} />

关于react-native - react native 复选框不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51251827/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com