I'm using React and Firestore.
I want to get the value recorded in Firestore and setState
to the retrieved value.
The current code is as follows.
const [value, setValue] = useState("");
useEffect(() => {
db.collection("value")
.doc(uid)
.collection("subvalue")
.onSnapshot(async (snapshot) => {
snapshot.forEach((doc) => {
const rawValue = doc.data().raw_value;
setValue(rawValue);
console.log(value);
});
});
}, []);
return <p>{value}</p>
In this code, value
is empty.
Checking the console, it is also empty. (It doesn't show up as null or undefind, just empty.
console
However, when I ran console.log(rawValue)
in forEach
, the value was displayed in the console.
useEffect(() => {
db.collection("value")
.doc(uid)
.collection("subvalue")
.onSnapshot(async (snapshot) => {
snapshot.forEach((doc) => {
const rawValue = doc.data().raw_value;
console.log(rawValue);
setValue(rawValue);
});
});
}, []);
console
foo
This shows that the value is not empty, but exists, and that we are setState
the value that exists.
I feel like I'm making some elementary mistakes, but I think what I'm doing is simple.
- Get the value ← Yes.
- value is not empty ← That's right.
- Set the value ← Not done.
What's going on in the last step?