pbeshai / react-url-query

A library for managing state through query parameters in the URL in React
https://pbeshai.github.io/react-url-query
MIT License
194 stars 38 forks source link

Put URL props into a urlParams object in props #43

Closed lemmingapex closed 6 years ago

lemmingapex commented 6 years ago

This is a great library, thanks. I am wondering what the easiest way to put all the url props in a child object of props might look like. Accessing a urlProp of name might look like:

props.urlParams.name

rather than

props.name

Is it possible to do this entirely via urlPropsQueryConfig?

Thanks in advance!

pbeshai commented 6 years ago

Hmm. This can't be done entirely via the urlPropsQueryConfig, but you can add in mapUrlToProps function for this purpose. I think this would work:

function mapUrlToProps(urlProps, props) {
  return { 
    ...urlProps, // expand all url props for normal behavior (optional)
    urlParams: urlProps  // gather all url props into a single object
  };
}

This would add a new prop called urlParams to the props and it would contain the object of all url query params from the config.

To use it, you would do something like:

export default addUrlProps({ mapUrlToProps, urlPropsQueryConfig })(MainPage);

Let me know if this works!

lemmingapex commented 6 years ago

Worked like a charm! Thank you.