I am creating a hook to fetch all properties from a database. the function is asynchronous, when I call the hook in another function, it is []. I know that the function to fetch from the database is asynchronous, I understand that it has to wait for the server for a response. I don't fully understand it well, how do I get the hook to return the data I need?
I used this same logic in another function to fetch properties and it worked, however, I want to put it in a hook because I will be using it in many other parts of the program. Please don't be mean and condescending with the responses, I am learning that's why I come here to ask.
const getAllProperties = async () => {
const propertyRef = ref(database, "properties");
const getProperties = async () => (await get(propertyRef)).val();
const [allProperties, setAllProperties] = useState([]);
getProperties().then((properties) => {
setAllProperties(properties ? Object.values(properties) : []);
});
console.log(allProperties);
return allProperties;
};