如何将按键中的键分配给状态变量

时间:2019-05-08 10:08:45

标签: javascript android reactjs react-native

我有下面的代码,我想使用按钮中的键值进行发布请求。如何将所选按钮键的值放在要发布的状态变量中?变量为id_troti,应获取按钮的键值。

makeBooking() {
        fetch('https://porsche.e-twow.uk/reactnative/add_rezervare.php', {
            method: 'POST',
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
                'Cache-Control': 'no-cache, no-store'
            },
            body: JSON.stringify({
                start: this.state.startDate,
                end: this.state.endDate,
                username: this.state.loggedUser,
                id_troti: 2
            })
        })
        .then(res => res.text())  // convert to plain text
        .then(text => console.log(text))
            .catch((error) => {
                console.error(error);
            });
    }

<TouchableOpacity style={styles.container} key={item.id} onPress={
  this.makeBooking.bind(this)
                                }>
                                    <Text style={styles.scooterText}>Scooter-ul {item.id}</Text>
                                </TouchableOpacity>

1 个答案:

答案 0 :(得分:0)

makeBooking = id=> {
        fetch('https://porsche.e-twow.uk/reactnative/add_rezervare.php', {
            method: 'POST',
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
                'Cache-Control': 'no-cache, no-store'
            },
            body: JSON.stringify({
                start: this.state.startDate,
                end: this.state.endDate,
                username: this.state.loggedUser,
                id_troti: id
            })
        })
        .then(res => res.text())  // convert to plain text
        .then(text => console.log(text))
            .catch((error) => {
                console.error(error);
            });
    }

<TouchableOpacity style={styles.container} key={item.id} onPress={e=>
  this.makeBooking(item.id)
                                }>
                                    <Text style={styles.scooterText}>Scooter-ul {item.id}</Text>
                                </TouchableOpacity>