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

Should loading become true by default? #94

Open
kykungz opened this issue Nov 5, 2019 · 3 comments
Open

Should loading become true by default? #94

kykungz opened this issue Nov 5, 2019 · 3 comments
Labels
enhancement New feature or request

Comments

@kykungz
Copy link

kykungz commented Nov 5, 2019

I noticed loading in useAxios is initially set to false, which causes bug in my program.

For example:

const { data, loading, error } = useAxios({ url: API_URL })

return loading
    ? <div>loading...</div>
    : <div>{data.firstName}</div>

It is causing bug because loading is initially set to false, so <div>{data.firstName}</div> is being rendered and got firstName of undefined.

@kykungz
Copy link
Author

kykungz commented Nov 19, 2019

@zakangelle any progress or opinion on this?

I prefer having loading initially set to true because I came from @apollo/react-hooks, which have loading of true when it first renders, so we only need to guard the loading instead of both loading and data.

@therealparmesh
Copy link
Collaborator

therealparmesh commented Dec 18, 2019

I can see the case for loading being true by default for useAxios, but not for useLazyAxios.

@zxqx
Copy link
Member

zxqx commented Dec 18, 2019

Yeah, I think I agree with this - let's do it.

Also, it doesn't look like you're using TS in this app, but something to be aware of: in your code example, having loading be true initially would not make accessing data.firstName typesafe.

You would either need a runtime check:

<div>{data && data.firstName}</div>

or a default value for data:

const { data = {}, loading, error } = useAxios({ url: API_URL });

@zxqx zxqx added the enhancement New feature or request label Dec 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants