Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using key in attrs causes onupdate. Expected oninit #2650

Closed
MarcWeber opened this issue Dec 31, 2020 · 7 comments
Closed

using key in attrs causes onupdate. Expected oninit #2650

MarcWeber opened this issue Dec 31, 2020 · 7 comments
Labels
Type: Question For issues that are purely questions about Mithril, not necessarily bug reports or suggestions

Comments

@MarcWeber
Copy link

dev or stable mithril: https://mawercer.de/tmp/tmp/mithriltest.html

              'key': attrs.type,

I do set key, yet onupdate is called. Why ?
If I change :type to :key also replacing attrs.type by attrs.key then oninit gets called.

Setting a different key shouldn't it cause recreating the component ?

So what am I missing ?

@MarcWeber MarcWeber added the Type: Question For issues that are purely questions about Mithril, not necessarily bug reports or suggestions label Dec 31, 2020
@cavemansspa
Copy link
Contributor

cavemansspa commented Dec 31, 2020

hello marc, i ported your example to flems so that it's easier to collaborate.

check out @osban re-initializing examples

you can probably benefit to moving to using route-resolvers with your routing.

-- UPDATE --

changing line 68 to }, m(v, {key: attrs.type, ...attrs})); results in initialization.

@osban
Copy link
Contributor

osban commented Dec 31, 2020

@MarcWeber I agree with @cavemansspa's assessment. If you'd use a route resolver, you could use a Layout, and do something like this.

@MarcWeber
Copy link
Author

@cavemansspa Thing I don't get is: The :component passing different key to new component could have state. So why does using key on the the parent components make it work ? I mean I could be setting state and m.redraw, right ? The logging (on update) does show that the changed attrs get passed. What are plans for SSR?

Example is here: fleem

So I change the state (eg coud be stream/ var or like in this case a function).. I pass it as key.. Yet it results in onupdate ?
Shouldn't changing the key mean getting a new component ? I mean that's what its for ?

@cavemansspa
Copy link
Contributor

@MarcWeber -- see doc

there's a subtlety where you need to wrap with []. that should resolve.

original flem modified

@MarcWeber
Copy link
Author

Ok - thank you very much. Still looks too complicated to me. The documentation could be more clear by making the array parentheses red or so if they are the most important thing which doesn't make sense to me at all. For me [x] or x was the same. My brain doesn't want to accept this. I have seen that example in docs but totally (intentionally) missed the array cause doesn't make sense to me.

My original case is solved. But I personally tend to see it as bug violating KISS principle which mean keep it stupid simple. Mithril gets much right though. Up to you whether you want to close this or not.

@pygy
Copy link
Member

pygy commented Jan 16, 2021

@MarcWeber this is related to #2654

Keys are only detected in children lists, not in the vnodes returned by components. If you wrap the vnode in an array, Mithril treats it as the child of a keyed fragment and it is re-initialized as you wish.

Going from

return m(C_item_new, {
  'type': attrs.type,
  'key': attrs.type
})

to

return [
  m(C_item_new, {
    'type': attrs.type,
    'key': attrs.type
  })
]

@barneycarroll
Copy link
Member

☝️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Question For issues that are purely questions about Mithril, not necessarily bug reports or suggestions
Projects
Status: Closed
Development

No branches or pull requests

5 participants