Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncorrect previous params if using callback inside setQuery in case of using urlName #249

Open
Resetand opened this issue Nov 4, 2022 · 3 comments

Comments

@Resetand
Copy link

Resetand commented Nov 4, 2022

Hi, thanks for those amazing libraries.
One of the last release contains many really useful updates. urlName was a lifesaver for me because it allowed me to get rid of a lot of workarounds.

And it's seems like i found a bug

Problem

The problem occurs when using urlName

In this example prev object inside setQuery callback will never contain paramWithAlias property, however the query state object will be up to date

const StringParamWithAlias = {...StringParam, urlName: 'alias'}

// URL https:[...]?alias=example&other=here
const [query, setQuery] = useQueryParams({ paramWithAlias: StringParamWithAlias, other: StringParam })

useEffect(() => {
    setQuery(prev => {
        console.log(prev) // ! will be { other: 'here' } without `paramWithAlias`
        return ...
    })
},[])
@Resetand
Copy link
Author

Resetand commented Nov 4, 2022

This is my current workaround in case anyone needs it

const useQueryParamsWrapper = (paramsConfigMap, options) => {
    const [params, setParams] = useQueryParams(paramsConfigMap, options);

    const paramsRef = useRef(params);
    paramsRef.current = params;

    const setParamsWrapped = useCallback(
        (action, updateType) => {
            const newParams = action instanceof Function ? action(paramsRef.current) : action;
            setParams(newParams);
        },
        [setParams],
    );

    return [params, setParamsWrapped];
};

@gregkepler
Copy link

@Resetand Thanks for this workaround. One question - where does "is.Function()" come from? I haven't encountered that yet myself.

@Resetand
Copy link
Author

Resetand commented Dec 5, 2022

@Resetand Thanks for this workaround. One question - where does "is.Function()" come from? I haven't encountered that yet myself.

It's an internal util. You can use action instanceof Function instead 🙃
PS Updated workaround comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants