deep fetch/observe relations
Hi!
Is there a way to connect with React components and observe deep relations? I need something like this:
const enhance = withObservables(['post'], ({post}) => ({
post: post.observe(),
author: post.author.observe(),
authorCat: post.author.cat.observe() // Just to illustrate, I am out of creativity here 🐱
});
export default enhance(PostComponent);
If not, what do you guys recommend in cases like this? I could probably use another component inside "PostComponent" to enhance and observe the cat inside, since author would be already available, but I am refactoring an app and it could result in a LOT of work.
Thanks!
I could probably use another component inside "PostComponent" to enhance and observe the cat inside, since author would be already available
Hi @jon-dearaujo! This is correct approach - you can enhance one withObservables with another.
const enhancePost = withObservables(['post'], ({post}) => ({
post: post.observe(),
author: post.author.observe()
});
const enhanceAuthorCat = withObservables(['author'], ({author}) => ({
authorCat: author.cat.observe()
});
export default enhancePost(enhanceAuthorCat(PostComponent));
You may also use compose-like function (i.e. from recompose https://github.com/acdlite/recompose).
const enhance = compose(
withObservables(['post'], ({post}) => ({
post: post.observe(),
author: post.author.observe(),
}),
withObservables(['author'], ({author}) => ({
authorCat: author.cat.observe()
})
);
export default enhance(PostComponent);
Or you can use switchMap operator from rx/js if you don't need author directly.
withObservables(['post'], ({post}) => ({
post: post.observe(),
authorCat: post.author.observe().pipe(switchMap((author) => author.cat.observe())),
}),
Thanks, @rkrajewski! It is a really good solution.
@jon-dearaujo If you can spare 15 minutes, you could help others by contributing to the documentation and explaining this case :)
I can do it and submit the PR later today, probably patching docs/Components.md with this tip in the advanced section. Sounds good?
Sounds great!
I could probably use another component inside "PostComponent" to enhance and observe the cat inside, since author would be already available
Hi @jon-dearaujo! This is correct approach - you can enhance one
withObservableswith another.const enhancePost = withObservables(['post'], ({post}) => ({ post: post.observe(), author: post.author.observe() }); const enhanceAuthorCat = withObservables(['author'], ({author}) => ({ authorCat: author.cat.observe() }); export default enhancePost(enhanceAuthorCat(PostComponent));You may also use compose-like function (i.e. from recompose https://github.com/acdlite/recompose).
const enhance = compose( withObservables(['post'], ({post}) => ({ post: post.observe(), author: post.author.observe(), }), withObservables(['author'], ({author}) => ({ authorCat: author.cat.observe() }) ); export default enhance(PostComponent);Or you can use
switchMapoperator fromrx/jsif you don't needauthordirectly.withObservables(['post'], ({post}) => ({ post: post.observe(), authorCat: post.author.observe().pipe(switchMap((author) => author.cat.observe())), }),
One thing I forgot to mention is that the relation, in my case, of post -> author is optional.
So I ended up with the switchMap solution along with a of in case of empty author.
const enhance = withObservables(['post'], ({post}) => ({
post: post.observe(),
author: post.author.observe(),
authorCat: post.author.observe().pipe(switchMap((author) => author ? author.cat.observe() : of(null)))
}))
The double-enhance solution was not working without the of from rxjs, I am not sure what i should return when the author is not present in the second enhancer.
const enhanceAuthorCat = withObservables(['author'], ({author}) => ({
authorCat: author ? author.cat.observe() : null
});
and
const enhanceAuthorCat = withObservables(['author'], ({author}) => {
return author ? {authorCat: author.cat.observe()} : null
};
are not valid getObservables functions. It would need to be like
const enhanceAuthorCat = withObservables(['author'], ({author}) => ({
authorCat: author ? author.cat.observe() : of(null) // of from rxjs
});
Anyway, the switchMap seems to be the simplest way, even with all the rxjs not so friendly code.
authorCat: author ? author.cat.observe() : of(null)
This is correct and simplest. You can Simplify to author ? author.cat : of(null). I think It would be reasonable if passing null in withObservables would just inject null as props. I can give you some pointers how to do it if you’d like to contribute. Then you’d get down to authorCat: author?.cat
#161