You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It's important that the observable returned by `getObservable` shouldn'tmaketheside-effect (orrequest) onsubscription-Instead, itshouldtaketheneededvaluefromotherexistingstreams. See [CoreConcepts-instances](core-concepts.md#instances) formoreinfo
And of course, this will work: Any component can use `usePost` to fetch the post of a specific id.
163
+
And of course, this will work: Any component can use `useTodos` to get the list of todos.
164
164
165
165
However, there are some times where we need to use data coming directly from the user. This is where RxJS `Subject`s come into play.
166
166
@@ -188,6 +188,28 @@ Keep in mind that `bind` doesn't do magic. If no one is subscribed to `todoList$
188
188
189
189
Remember, if you have a case like this (where you are pushing a Subject but no one is subscribed to those changes), make sure you have an active subscription to the stream by using `<Subscribe source$={stream} />` or `useSubscribe(stream)`. This way, `todoList$` will update when a new value is pushed to the subject, and the result will be replayed for every new subscriber that arrives later.
190
190
191
+
## Instances
192
+
193
+
There are many times where you need components to access a particular instance - Classic example is a list of posts. To help with that, `bind` can also take a factory function that returns an Observable for that instance.
194
+
195
+
However, when using this overload it's important that this observable shouldn't make the actual request, because it's hard to manage the lifecycle of these observables (when they subscribe, and when they can clean up) - Instead, it should take the value from an existing Observable-state.
196
+
197
+
For example, if we have a list of posts, we might have an observable that has all of them in a dictionary:
Although from within each instance component we could theoretically call `usePosts()`, and then take the post that component actually needs, this would cause unnecessary re-renders when other instances change. We solve this by using the factory overload:
204
+
205
+
```ts
206
+
const [usePost, post$] =bind((id:string) =>
207
+
posts$.pipe(map((posts) =>posts[id])),
208
+
)
209
+
```
210
+
211
+
And now the component can use `usePost(id)` by passing it's own id, and that component will re-render only when that post changes. The second parameter returned, `post$`, it's actually also a function so that it can be composed in other streams: `post$(id)` returns the observable instance that emits Posts for that specific id.
You might be wondering - how does this _exactly_ work with React? If React is pull-based and it _needs_ a value at the time it's re-rendering, this stream won't have a value until the ajax call is resolved.
@@ -212,9 +234,7 @@ import { ajax } from "rxjs/ajax"
Now `usePost` will emit `null` immediately while it's fetching data (so that we can manually handle that), instead of suspending the component, and when the ajax call is resolved, it will emit the result of that call.
0 commit comments