A React.js High-Order Component and decorator for parsing and resolving URLs inside your application.
npm i -S react react-urlimport{render}from'react-dom';import{URLProvider}from'react-url';importAppfrom'./containers/App';consturls={profile: '/profile/:username/',};render(<URLProviderurls={urls}><App/></URLProvider>,document.body,);URLProvideris a High-Order Component.URLProviderexpect only one property namedurls.urlsshould be an object where the keys are the URLs names and the values are the unparsed url using the syntax of Express.js.
importReact,{Component}from'react';import{connectURL}from'react-url';functionmapURLToProps(getURL,props){return{profileURL: getURL('profile',{username: props.username}),};}
@connectURL(mapURLToProps)classUserDataextendsComponent{ ... }exportdefaultUserData;- The
connectURLargument (mapURLToProps) it's optional. - If you don't supply it then it will add the
getURLfunction as a property. - The
mapURLToPropsfunction will receive thegetURLfunction andpropsobject as parameter and should return an object. - The
getURLfunction receive the URL name and an object with the parameters to use in it and return the parsed URL. - You can use it as a decorator (like the example above) or just as a function and send them the component to connect.
import{parser}from'react-url';consturls={profile: '/profile/:username/',};constprofileURL=parser(urls,'profile',{username: 'sergiodxa',});- This is a Low-Level API and is used internally for the
connectURLdecorator, it's not expected that you use it directly. parserreceive as arguments theurlsmap, the URL name and the options/parameters object.- It will return the final parsed url string.