React Native - 按ID更新和删除

问题描述 投票:0回答:1

我点击了更新和删除按钮,尝试按ID更新和删除某些项目。但我的问题是,我不确定采用“用状态(或任何东西)获取ID”的正确方法是什么。

  • 有了这些方法,我就有错误。 (例如:undefined不是对象......)
  • 没有这些,我什么都没有。我很清楚这一点,因为我没有提到任何东西。所以我必须使用ID。但我不知道这个方法。

请帮助,任何事情都将不胜感激。 :)

这是我的代码

Settlement.js

export default class Settlement extends Component {
    constructor(props){
    super(props)
        this.state = {
            ...
            deleteOrder: '',
            numOrder: '',
        };
    }

    submit = () => {
        this.state.numOrder = item.order_id;     // here's my fetch with state
        fetch('http://192.168.254.***:****/SendOrder/update' + this.state.numOrder, {
            method: 'PUT',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                spcl_req: this.state.SplOrder,
                order_quantity: this.state.count,
                order_type: this.state.DineIn,
            })
        }).then(res => res.json())
            .then((responseJson) => {
                Alert.alert(JSON.stringify(responseJson))
                console.log(responseJson);
            })
        .catch(error => console.error('Error:', error))
    }
    delete = () => {
        this.state.deleteOrder = item.order_id;     // here's my fetch with state
        fetch('http://192.168.254.***:****/SendOrder/delete_order/' + this.state.deleteOrder, {
            method: 'DELETE',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                order_id: this.state.deleteOrder
            })
        }).then((responseData) => {
            Alert.alert(JSON.stringify(responseData))
            console.log(responseData.rows)
        }).done();
    }

    render() {
        const { params } = this.props.navigation.state;
        return (
            <View>
                <FlatList
                ...
                renderItem = {({ item }) =>
                    <View>
                        ....
                        <View>
                            <TouchableOpacity
                                onPress = { () => this.submit() }>
                                <Text>UPDATE</Text>
                            </TouchableOpacity>
                            <TouchableOpacity
                                onPress = { () => this.delete() }>
                                <Text>REMOVE</Text>
                            </TouchableOpacity>
                        </View>
                    </View>
                }/>
            </View>
        )
    }
}[![enter image description here][1]][1]
javascript android reactjs react-native fetch
1个回答
0
投票

你在React中打破了几个概念。

this.state.numOrder = item.order_id;此行不会更新您的州

这一行中的项目是什么? renderItem = {({item})=>

也许在这个对象中有一个标识符,必须作为参数传递给你的函数

© www.soinside.com 2019 - 2024. All rights reserved.