0

I am fetching a list of objects from a database, but it is not saving the items in state.

when i logged the individual properties they show, but when i try to log allProperties before the return statement, it is empty

const getAllProperties = () => {
const propertyRef = ref(database, "properties");

const getProperties = async () => (await get(propertyRef)).val();
const [allProperties, setAllProperties] = useState([]);

getProperties().then((properties) => {
  Object.values(properties).forEach((property) => {
    setAllProperties([property]);
    // console.log(property);
  });
});
console.log(allProperties);

return allProperties;

};

lams
  • 352
  • 1
  • 10
  • Setting state is asynchronous. – Dave Newton Apr 23 '23 at 19:12
  • This is a weirdly structured attempt at a hook, and it looks like you're trying to wrap an asynchronous operation in as many things as you can in some weird hope that it will stop being asynchronous... – David Apr 23 '23 at 19:15

0 Answers0