bradstiff / react-app-location

A package to avoid repetition with Routes and URLs, and reduce boilerplate with location param parsing in React Apps
MIT License
97 stars 8 forks source link

Option not to render link on fail #3

Closed YM-KA closed 5 years ago

YM-KA commented 5 years ago

When generating links by iterating data it would sometimes be preferred to be able tom omit links that do not match the schema. Now the whole page breaks with giving an error along the lines of "Warning: validateParams: id must match the following:"/-\w{19}/". See code excerpt below.

...
//Definition
const someId = Yup.string().matches(/-\w{19}/)
export const MyLocation = new Location('/my/:id', {id : someId .required()})

...
//Link generation in component
props.things.map((my) => {
  return <li key={my.id} >
  ...
  { MyLocation.toLink( my.title, {id: my.id} ) }
  ...

If any single link fails validation the entire page breaks. The error stack shows:

Alternatively - would it be possible to define a default value that gets presented when validation fails?

Kind regards Kazuyuki

bradstiff commented 5 years ago

I could add an isValidParams method to the Location that you could use to conditionally render the link, or something else when invalid.

YM-KA commented 5 years ago

Thank you for the prompt reply! Yes, please - that would be fantastic! 👍

bradstiff commented 5 years ago

Implemented in v1.2.0, now on npm.

YM-KA commented 5 years ago

Many thanks, works like a charm!