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

Default value for data, possible? #181

Closed
fabn opened this issue Feb 27, 2020 · 2 comments
Closed

Default value for data, possible? #181

fabn opened this issue Feb 27, 2020 · 2 comments
Labels
wontfix This will not be worked on

Comments

@fabn
Copy link

fabn commented Feb 27, 2020

Hi, thanks for your little gem. I was able to simplify my code from

export default function InvoicesDashboard() {
    const [isLoading, setIsLoading] = useState(false);
    const [invoices, setInvoices] = useState([]);

    useEffect(() => {
        setIsLoading(true);
        api.get(`/invoices`)
            .then((data) => setInvoices(data))
            .finally(() => setIsLoading(false))
    }, []);
    
    // component rendering
    return <InvoicesTable invoices={invoices}/>
}

to this

export default function InvoicesDashboard() {
    const [{data: invoices, loading}] = useApi('/invoices');
    
    return <InvoicesTable invoices={invoices || []}/>;
}

However I need to manually handle first render when data are not already loaded with invoices || [].

I was wondering if there's any way to specify a initial value for data as in useState hook. If not it would be a great addition to this project.

Thanks.

@simoneb
Copy link
Owner

simoneb commented Feb 27, 2020

This is not supported and will probably not be, considering how easy handling this case in user code is. See previous discussion in #110 and #26

@simoneb simoneb closed this as completed Feb 27, 2020
@simoneb simoneb added the wontfix This will not be worked on label Feb 27, 2020
@fabn
Copy link
Author

fabn commented Feb 27, 2020

Thanks, I missed those ones. I solved with

    const [{data: invoices = [],  loading}] = useApi('/invoices');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants