I've been struggling with this from yesterday, and couldn't solve it.
Basically, we are making a website for a restaurant, which customer can use to order foods and pay for it. The feature I'm implementing now is, when user press the checkout button, it goes to /order_confirm and display all the menus ordered. I'm trying to pass the order number by setting the url as /order_confirm/id where id is the order number. Everything works fine, except that when I do setState() to change the value of the ordId which is state and pass the state value to the according class, it seems like setstate not changing the value at all.
handleCheckoutRequest = async () => {
const { tableNumber } = this.state;
const orderOrdered = 1;
const menuItemIds = this.props.menuItems.map((a) => a.id);
const menuItemCounts = this.props.menuItems.map((a) => a.count);
await API.post(`/orders/`, null, {
params: { tableNumber, orderStatus: orderOrdered },
})
.then(async (response) => {
const orderId = response.data.id; // this works
await API.post(`/ordered_items/${orderId}`, null, {
params: { menuItemIds, menuItemCounts },
})
.then((response) => {
this.setState({
responses: ["Successfully added order items"], // this works
responseStatus: "success", // this works as well
ordId: orderId, // this doesn't
});
console.log(orderId); // display 25 on the console (which is right)
console.log(this.state.ordId); // display -1 which is the default value
})
.catch((error) => {
console.log(error);
});
})
};
I get this error in console.
Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
I've tried setting the tempo, using the arrow function in the setState(), declaring method outside and using the method to change the value. It all didn't work.
This is handleCheckout method.
handleCheckout = () => {
const { menuItems } = this.props;
const { tableNumber } = this.state;
let responses = [];
if (tableNumber === -1 || !tableNumber)
responses.push("No table has been selected");
if (menuItems.length < 1)
responses.push("No menu items have been added to cart");
if (responses.length > 0) {
this.setState({ responses, responseStatus: "fail" });
return;
}
this.setState({ responses: [], responseStatus: "" });
this.handleCheckoutRequest();
};
this is the part where I'm passing the state as props.
<div className="cart__bottom">
<Checkout
id={this.state.ordId}
onTableChange={this.handleTableChange}
onCheckout={this.handleCheckout}
responses={responses}
responseStatus={responseStatus}
/>
</div>
This is render() of where it's passed
render() {
const { id, onTableChange, onCheckout, responses, responseStatus } = this.props;
return (
<div className="cart__checkout">
<Response responses={responses} responseStatus={responseStatus} />
<input
className="input mb-2"
type="number"
name="tableNumber"
placeholder="Table Number"
onChange={onTableChange}
/>
<Link to={{
pathname: "/order_confirm/" + id
}}>
<button
className="button is-primary is-fullwidth"
onClick={onCheckout}
>
Checkout
</button>
</Link>
</div>
);
}
I also found an article that seems like it could be a solution to my problem. Only barrier is that I'm not good enough with React to apply the solutions.
Avoid React state update warnings on unmounted components