AaronErhardt / AaronErhardt.github.io

My personal blog
https://aaronerhardt.github.io
3 stars 4 forks source link

Update components.md #30

Closed tronta closed 3 years ago

tronta commented 3 years ago

What about this? Maybe even, for good practice, we want to store the applications state centrally.

AaronErhardt commented 3 years ago

Actually, we want to store as much state as possible in components. But in this case it's not necessary. So it has not directly something to do with simplicity. There's simply nothing that can be stored in the component.

tronta commented 3 years ago

Wouldn't it also be possible to store which button has been pressed last and the root component reads it from there?

AaronErhardt commented 3 years ago

Yes that would be possible, it's not as simple because of Rust's ownership rules. In this case you'd need to send a message to the component to ask for its state and wait for a response. Or you use Rc<RefCell> to have shared state between both models. Maybe I'll add a chapter to the book for shared state because sometimes it's not an option to send messages but in this case using messages should be the simples solution.

tronta commented 3 years ago

That would be great. I usually stumble over absolute sentences like 'there is no state' and I start thinking why is there no state... couldn't there be one...?! So a kind of reasoning would be great. In this case we could say we want only a stateless controlling component or such...

AaronErhardt commented 3 years ago

I'll have a look at this section when I'm back. You're right, there should be a bit more information why it's stateless.

AaronErhardt commented 3 years ago

This part should now have a better explanation: https://aaronerhardt.github.io/relm4-book/book/components.html#the-model