unfold/react-firebase

Improve connect usage

BrodaNoel opened this issue ยท 4 comments

Huy guys!

What if instead of doing:

export default connect((props, ref) => ({
  value: 'counterValue',
  setValue: value => ref('counterValue').set(value)
}))(Counter)

we should do:

export default connect((props, ref) => ({
  value: ref('counterValue'),
  setValue: value => ref('counterValue').set(value)
}))(Counter)

I guess it keep it more consistent and readable.
Internally, you should be able to receive a ref value or a string.
And do something like:

if (typeof x === 'string')
  x = ref(x); // if received an string (as it currently works), transform if as a ref object.
}

Great point, maybe we should support refs as well. You can accomplish everything without it, but it might be more familiar to firebase veterans.

Your suggestion of typeof x === 'string' would not work as we also support objects as a query, and functions as action handlers. Been trying to find the ref() instance so that we could to if (subscription instanceof firebase.database.ref) but firebase is not open source ๐Ÿ’ฉ so hard to find.

Maintainers on holiday โ˜€๏ธ๐Ÿน, so if you have a suggestion please submit a pull request. Cheers ๐Ÿบ

I have a couple of pending work to do in my personal projects, but I promise to send some PR if I'm free in these next days.

Thanks for this library, it has been a great time-saver on a project i'm working on! kudos to the unfold team and other contributors ๐Ÿ˜„. Hope u guys had a nice holiday! ๐Ÿป

@einarlove guess you are looking for one of:

spoiler ๐Ÿ˜œ

class Reference extends Query

I just don't know which one is the correct choice for this case, as i'm not an expert on firebase.

testing ๐Ÿ’ญ

i did a quick test on the console of a dummy firebaseapp.com domain and this worked:

const subscription = firebase.database().ref('users'); // or any path
subscription instanceof firebase.database.Query // => true
subscription instanceof firebase.database.Reference // => true

const object = {}
object instanceof firebase.database.Query // => false
object instanceof firebase.database.Reference // => false

PS

the firebase sdk is now open-sourced here.
(altough it's written in typescript).

Closing this issue. Reopening if this reappears as a feature request.