IjzerenHein / firestorter

Use Google Firestore in React with zero effort, using MobX 🤘
http://firestorter.com
MIT License
378 stars 50 forks source link

access doc.data outside of render()? #14

Closed ckjpdx closed 6 years ago

ckjpdx commented 6 years ago

Hello! Firestorter is awesome!

I'm trying to use information from a document.data reference outside of a render function. I'm displaying that info in render in several places on my app, and it's wonderful, but when I try to access doc.data in a onHandle function, for example, it returns the observable, or doc.data.someData will return undefined.

Is there some way to get info in JSON form outside of render or should I look to vanilla firestore api for this?

Thanks!

IjzerenHein commented 6 years ago

Hi Chris,

What I think what's going on, is that doc.data is not being accessed from the render function. If that is not the case then firestorter will not consider the collection/document to be observed and not start real-time updating for it. Could you add a call console.log(document.data); to your render function and verify whether that fixes the problem?

cheers, Hein

cworf commented 6 years ago

If you want to use observable data outside render, you need to send it to the function or method via curried function inside render. for example

<div> onClick={() => this.someMethod(document.data)} </div>

Are you trying to access data on mount? then you should send it to the component via props and access it inside componentDidMount()

ckjpdx commented 6 years ago

Yes, I needed to incorporate render() when working with any observable data. Thanks!