Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React Native-按ID更新和删除_Javascript_Android_Reactjs_React Native_Fetch - Fatal编程技术网

Javascript React Native-按ID更新和删除

Javascript React Native-按ID更新和删除,javascript,android,reactjs,react-native,fetch,Javascript,Android,Reactjs,React Native,Fetch,点击/点击更新和删除按钮后,我正试图通过ID更新和删除一些项目。但我的问题是,我不确定什么是正确的方法来“使用状态对ID进行获取” 使用这些方法,我会有错误。(例如:未定义不是对象…) 没有这些,我什么都没有。我很清楚这一点,因为我指的不是任何东西。所以我必须使用ID。但我不知道该怎么做 请帮忙,我们将不胜感激。:) 这是我的密码 结算.js export default class Settlement extends Component { constructor(props){

点击/点击更新和删除按钮后,我正试图通过ID更新和删除一些项目。但我的问题是,我不确定什么是正确的方法来“使用状态对ID进行获取”

  • 使用这些方法,我会有错误。(例如:未定义不是对象…)
  • 没有这些,我什么都没有。我很清楚这一点,因为我指的不是任何东西。所以我必须使用ID。但我不知道该怎么做
请帮忙,我们将不胜感激。:)

这是我的密码

结算.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]
导出默认类结算扩展组件{
建造师(道具){
超级(道具)
此.state={
...
删除顺序:“”,
努莫德:“,
};
}
提交=()=>{
this.state.numOrder=item.order\u id;//这是我的状态获取
取('http://192.168.254.***:***/SendOrder/update'+this.state.numOrder{
方法:'放',
标题:{
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”
},
正文:JSON.stringify({
spcl_请求:this.state.SplOrder,
订单数量:this.state.count,
订单类型:this.state.DineIn,
})
}).then(res=>res.json())
.然后((responseJson)=>{
Alert.Alert(JSON.stringify(responseJson))
console.log(responseJson);
})
.catch(error=>console.error('error:',error))
}
删除=()=>{
this.state.deleteOrder=item.order\u id;//这是我的状态获取
取('http://192.168.254.***:*****/SendOrder/delete_order/'+this.state.deleteOrder{
方法:“删除”,
标题:{
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”
},
正文:JSON.stringify({
订单标识:this.state.deleteOrder
})
}).然后((响应数据)=>{
Alert.Alert(JSON.stringify(responseData))
console.log(responseData.rows)
}).完成();
}
render(){
const{params}=this.props.navigation.state;
返回(
....
this.submit()}>
更新
this.delete()}>
去除
}/>
)
}
}[![在此处输入图像描述][1][1]

您在React中打破了几个概念

this.state.numOrder=item.order\u id; 此行不会更新您的状态

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


可能在这个对象中有一个标识符,它必须作为参数传递给函数

来自“this.state.numOrder”的“item”来自我的api。对于“renderItem={({item})=>”我使用它来声明api中的一些数据,我只是没有在这里显示它,因为其他人可能会对它感到困惑,代码会变得很长。尝试将它作为参数传递给函数,它应该如下所示:onPress={()=>this.submit(item.order\u id)}>在函数中:submit=id=>{fetch(
url/${id}
…etc那么,我应该在回迁中保留“this.state.numOrder=item.order_id;”和“+this.state.numOrder”吗?在本例中,您可以不使用Store,如果需要其他方式更新状态,请参阅文档