mirror of
https://github.com/status-im/re-frame.git
synced 2025-02-24 07:48:14 +00:00
FAQ update
This commit is contained in:
parent
da8a094799
commit
267ff1e298
@ -13,16 +13,16 @@ You should NOT do this:
|
|||||||
....)))
|
....)))
|
||||||
```
|
```
|
||||||
|
|
||||||
If you do:
|
because that `subscribe`:
|
||||||
1. You might create a memory leak (the subscription might not be "freed")
|
1. might create a memory leak (the subscription might not be "freed")
|
||||||
2. You have just made you event handler impure (it reaches out and grabs a global value).
|
2. makes the event handler impure (it grabs a global value)
|
||||||
|
|
||||||
### The Better Way
|
### The Better Way
|
||||||
|
|
||||||
Instead, to make the value of a subscription available within an event handler, it has to
|
Instead, the value of a subscription should
|
||||||
be injected into the `coeffects` of that handler via an interceptor.
|
be injected into the `coeffects` of that handler via an interceptor.
|
||||||
|
|
||||||
The overall sketch is:
|
A sketch:
|
||||||
```clj
|
```clj
|
||||||
(re-frame.core/reg-event-fx ;; handler must access coeffects, so use -fx
|
(re-frame.core/reg-event-fx ;; handler must access coeffects, so use -fx
|
||||||
:event-id
|
:event-id
|
||||||
@ -32,11 +32,17 @@ The overall sketch is:
|
|||||||
....)))
|
....)))
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
1. `inject-sub` is an interceptor which will get the subscription value and add it to coeffects (somehow)
|
||||||
|
2. The event handler obtains the value from coeffects
|
||||||
|
|
||||||
|
So how to write this interceptor?
|
||||||
|
|
||||||
### Solutions
|
### Solutions
|
||||||
|
|
||||||
re-frame doesn't yet have a built in interceptor to do this injection.
|
re-frame doesn't yet have a builtin `inject-sub` interceptor to do this injection.
|
||||||
|
|
||||||
So you'll need to create your own or you may want to use this 3rd party library:
|
I'd suggest you use this 3rd party library:
|
||||||
https://github.com/vimsical/re-frame-utils/blob/master/src/vimsical/re_frame/cofx/inject.cljc
|
https://github.com/vimsical/re-frame-utils/blob/master/src/vimsical/re_frame/cofx/inject.cljc
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user