I would like to register a real-time update listener for Firestore and rerender it in React when there is an update. Asynchronous because the read API returns a promise, regardless of whether it listens for real-time updates or not. React uses this because it redraws at the timing of state updates. Result: A prototype was created that automatically changes the display in the browser when the value in the Firestore changes.
Stumbling Points
-
React.Component is a type function and takes the type of state as its second type argument
- If not given, it works as if
{}
were given- Definition:
interface Component<P = {}, S = {}, SS = any>
- Definition:
- The result is âReadonly<{}> has no piecesâ for
this.state.pieces
. - Property âpiecesâ does not exist on type âReadonly<{}>â.
- If not given, it works as if
-
The Firebase sample uses a function as the argument for onSnapshot, but as it is, it shadows this in Component.
- Visual Studio Code warned me so I didnât get stuck there.
- But I didnât understand the solution to this problem correctly.
- Since this will be shadowed and inaccessible.
- I figured I could just
let setState = this.setState
in the outer scope and use that.- Python-like conception; in Python, object methods are bound to objects.
- Thatâs not true in JS. this.setState is just a function
- Result TypeError: Cannot read property âupdaterâ of undefined
- In the past, JS would solve this problem by explicitly binding
const setState = this.setState.bind(this)
- Nowadays ES6 can be used that the arrow function does not bind (and therefore does not shadow) THIS at the time of the call.
- I used this one this time.
-
An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new.target keywords.
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
doubt
- I used constructor, but I see here and there people using componentDidMount. What is the difference?
typescript
This page is auto-translated from /nishio/éćæă§FirestoreăăèȘăă§Reactă§render using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. Iâm very happy to spread my thought to non-Japanese readers.