Tuesday, December 17, 2019

Passing params via Query Params

on sending page

const queryParams = [];
for (let i in this.state.ingredients) {
queryParams.push(
encodeURIComponent(i) +
'=' +
encodeURIComponent(this.state.ingredients[i])
);
}
const queryString = queryParams.join('&');
this.props.history.push({
pathname: '/checkout',
search: '?' + queryString
});

on receiving page

componentDidMount() {
const query = new URLSearchParams(this.props.location.search);
const ingredients = {};
for (let param of query.entries()) {
ingredients[param[0]] = +param[1];
}
this.setState({ ingredients: ingredients });
}
checkoutCancelledHandler = () => {
this.props.history.goBack();
};

No comments:

Post a Comment