react-url-query icon indicating copy to clipboard operation
react-url-query copied to clipboard

Value of param props is set to undefined instead of default value if invalid

Open ste93cry opened this issue 7 years ago • 1 comments

Do you want to request a feature or report a bug? Bug, but maintainers could say the current behavior is wanted

What is the current behavior? When using the addUrlProps HOC it's possible to configure for each URL field that should be managed a default value (note: I had to look at the source code to find out that this feature is available, I didn't found it anywhere in the docs). If the field is missing in the URL everything works fine and the wrapped React component will receive the props with the default value set. Also, (this is missing too from the docs) you can set a function callback that will be used to validate that the value of a field is valid. However, if it isn't undefined will be set instead of the default value taken from the configuration

https://github.com/pbeshai/react-url-query/blob/fb660e86940531ec8d626ae837d180c4e0d0bf66/src/urlQueryDecoder.js#L43-L49

What is the expected behavior? The decoded value assigned to the component props should be the default value of the parameter

Which versions of react-url-query, and which browser and OS are affected by this issue? Did this work in previous versions of react-url-query? The react-url-query version is 1.4.0, browser version is not important.

import React from 'react'
import ReactDOM from 'react-dom'
import { addUrlProps, UrlQueryParamTypes } from 'react-url-query'

class Hello extends React.Component {
    render() {
        return this.props.foo; // If in the URL the value is not "bar" this will be undefined instead of "baz"
    }
}

const HelloWithUrlProps = addUrlProps({
    urlPropsQueryConfig: {
        foo: {
            type: UrlQueryParamTypes.string,
            queryParam: 'foo',
            defaultValue: 'baz',
            validate(decodedValue) {
                return decodedValue === 'bar';
            },
        },
    },
})

ReactDOM.render(
  <HelloWithUrlProps />,
  document.getElementById('container')
);

ste93cry avatar Feb 23 '18 16:02 ste93cry

I think the solution to default values is to just use defaultProps or the language level default params. I will likely remove the defaultValue property in 2.0.

pbeshai avatar Mar 24 '19 00:03 pbeshai