【问题标题】:React Native - Update and Delete by IDReact Native - 按 ID 更新和删除
【发布时间】:2018-09-01 17:52:10
【问题描述】:

我正在尝试在点击/单击更新和删除按钮时按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】:

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

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

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

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

    【讨论】:

    • “this.state.numOrder”中的“项目”来自我的 api。而对于“renderItem = {({ item }) =>”,我用它来声明我的 api 中的一些数据,我只是没有在这里显示它,因为其他人可能会对此感到困惑并且代码会变长。跨度>
    • 尝试作为参数传递给函数它应该看起来像这样: onPress = { () => this.submit(item.order_id) }> 在你的函数中: submit = id => { fetch(url/${id} ...etc }
    • 我应该保留“this.state.numOrder = item.order_id;”然后在我的 fetch 中添加“+ this.state.numOrder”?
    • 在这个例子中,你可以不用商店
    • 而更新状态需要另一种方式,参见文档
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2023-02-01
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    相关资源
    最近更新 更多