#how to stop children elements from re-rendering when parent state change

1 messages · Page 1 of 1 (latest)

marsh yoke
#

I think my way of rendering children cause them to re-render each time parent state change which is not what I want

#[function_component]
pub fn Center() -> Html {
    let stylesheet = css!(/* */);
    let tx = &*use_state(|| channel::<AppMessage>(100).0);
    let (_, this_peer_dispatch) = use_store::<ThisPeer>();
    let other_peers = use_reducer_eq(|| OtherPeers::default());
    {
        let tx = tx.clone();
        let other_peers = other_peers.dispatcher();
        use_effect_with_deps(
            move |()| {
                connection_manager(other_peers, this_peer_dispatch, tx);
            },
            (),
        );
    }
   html! {
    <center class={stylesheet}>
        if other_peers.peers.is_empty() {
            <no-peers>
                <h3>{"Open Shrut on other devices to send files"}</h3>
            </no-peers>

        } else {
            <instruction class="smallfont">
                {"Tap or click to send a file"}
            </instruction>
            <peers class="center">
                {display_peers(other_peers.clone(), tx.clone())}
            </peers>
        }
    </center>
    }
}

fn display_peers(other_peers: UseReducerHandle<OtherPeers>, tx: Sender<AppMessage>) -> Vec<Html> {
    other_peers
        .peers
        .iter()
        .map(|peer| {
            html! {
                <Avatar id={peer.id.clone()} name={peer.name.clone()} os={peer.os.clone()} role={peer.role.clone()} tx={tx.clone()} />
            }
        })
        .collect()
}```
#

I'm thinking about draining the vector for each avatar that get rendered, but then how do I remove the avatar when it's peer disconnect

prisma olive
#

you forgot key= attribute wher you make Avatars in the list display

#

its the attribute that allows yew to know when to render an item again or when its the same item to compare to skip rendering

#

key can be anything, just ensure its unique for the list (so you can just repeat id value for the key's value for example)