Why are multi-methods not working as functions for Reagent/Re-frame?

前端 未结 4 1147
粉色の甜心
粉色の甜心 2021-02-12 12:53

In a small app I\'m building that uses Reagent and Re-frame I\'m using multi-methods to dispatch which page should be shown based on a value in the app state:

(d         


        
4条回答
  •  爱一瞬间的悲伤
    2021-02-12 13:31

    So, a component like this: [pages @some-ratom]
    will rerender when pages changes or @some-ratom changes.

    From reagent's point of view, pages hasn't changed since last time, it is still the same multi-method it was before. But @some-ratom might change, so that could trigger a rerender.

    But when this rerender happens it will be done using a cached version of pages. After all, it does not appear to reagent that pages has changed. It is still the same multimethod it was before.

    The cached version of pages will, of course, be the first version of pages which was rendered - the first version of the mutlimethod and not the new version we expect to see used.

    Reagent does this caching because it must handle Form-2 functions. It has to keep the returned render function.

    Bottom line: because of the caching, multimethods won't work very well, unless you find a way to completely blow up the component and start again, which is what the currently-top-voted approach does:
    ^{:key @current-route} [pages @current-route]
    Of course, blowing up the component and starting again might have its own unwelcome implications (depending on what local state is held in that component).

    Vaguely Related Background:
    https://github.com/Day8/re-frame/wiki/Creating-Reagent-Components#appendix-a---lifting-the-lid-slightly
    https://github.com/Day8/re-frame/wiki/When-do-components-update%3F

提交回复
热议问题