简体   繁体   中英

Navigation with react-router-dom

In react-router-dom there is an option to send data between pages in the url for example myBurger/?bacon=2&salad=3

My question is if there is a service that make the (bacon,salad) data string from an object... for example

var ingredients = {
  salad: 3,
  bacon: 2
}

myCustomServiceConverter.FromObjectToString(ingredients);
// return ?bacon=2&salad=3

And in the next page will be function that decoding it back

var URL = http://localhost:3000/myBurger/?bacon=2&salad=3
myCustomServiceConverter.FromStringToObject(URL);
// return { salad: 3, bacon: 2 }

please do not suggest those link

Yes you can do it by using a library named qs

You can find doc here

https://www.npmjs.com/package/qs

let url = qs.stringify({salad: 3,
  bacon: 2
})

and then getting back the object

using another library named query-string

https://www.npmjs.com/package/query-string

 let url = this.props.history.location.state.query;
 let object = queryString.parse(url, {arrayFormat: 'index'})

我已经为这种操作构建了一个小插件,也许您可​​以在这里尝试一下

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM