Mar 20, 2022 01:19 PM
I’ve built an app that allows users in my base to select a record in a certain table. I’m using GlobalConfig to store the ID of that record.
However, when I first publish the app to my base I get an error because the GlobalConfig does not yet have an ID stored. So when do useRecordById
, I get an error like this: “getRecordById expects a string” because there is no record ID stored in GlobalConfig, since a user has not yet selected one.
When I tried putting useRecordById
inside a conditional, I get an error because you can’t put hooks inside a conditional.
What is the best way to get around this? Thanks!
Solved! Go to Solution.
Mar 20, 2022 03:42 PM
Welcome to the Airtable community! And welcome to writing a custom app.
Since the rule of hooks says to put a hook (such as useRecordById) only at the top level of a component, you should break your code up into different components. If there is a stored record, call a component with useRecordById
. If there is no stored record, call a different component that lets the user pick the component.
Mar 20, 2022 03:42 PM
Welcome to the Airtable community! And welcome to writing a custom app.
Since the rule of hooks says to put a hook (such as useRecordById) only at the top level of a component, you should break your code up into different components. If there is a stored record, call a component with useRecordById
. If there is no stored record, call a different component that lets the user pick the component.
Mar 20, 2022 05:16 PM
Thanks @kuovonne ! That worked.