Skip to content

Too much boilerplate for components #830

@jstarry

Description

@jstarry

Is your feature request related to a problem? Please describe.
In order to create a component that has a bit of smarts in it, a developer needs to store the props, link, and probably some callbacks and state into the component struct.

Describe the solution you'd like
Each component lifecycle method should pass in a reference to the link and props to alleviate the burden on the developer to store those values themselves.

pub struct Context<COMP> {
    link: ComponentLink<COMP>,
    props: COMP::Properties,
}

pub trait Component: Sized + 'static {
    type Message: 'static;
    type Properties: Properties;

    fn create(ctx: &Context<Self>) -> Self;
    fn mounted(&mut self, ctx: &Context<Self>) -> ShouldRender;
    fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> ShouldRender;
    fn change(&mut self, ctx: &Context<Self>, props: &Self::Properties) -> ShouldRender;
    fn view(&self, ctx: &Context<Self>) -> Html;
    fn destroy(&mut self, ctx: &Context<Self>) -> ();
}

I believe this would also remove the need to have props implement cloneable

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions