Struct leptos::ForProps

source ·
pub struct ForProps<IF, I, T, EF, N, KF, K>
where IF: Fn() -> I + 'static, I: IntoIterator<Item = T>, EF: Fn(T) -> N + 'static, N: IntoView + 'static, KF: Fn(&T) -> K + 'static, K: Eq + Hash + 'static, T: 'static,
{ pub each: IF, pub key: KF, pub children: EF, }
Expand description

Props for the For component.

Iterates over children and displays them, keyed by the key function given.

This is much more efficient than naively iterating over nodes with .iter().map(|n| view! { ... })..., as it avoids re-creating DOM nodes that are not being changed.


#[derive(Copy, Clone, Debug, PartialEq, Eq)]
struct Counter {
  id: usize,
  count: RwSignal<i32>
}

#[component]
fn Counters() -> impl IntoView {
  let (counters, set_counters) = create_signal::<Vec<Counter>>(vec![]);

  view! {
    <div>
      <For
        // a function that returns the items we're iterating over; a signal is fine
        each=move || counters.get()
        // a unique key for each item
        key=|counter| counter.id
        // renders each item to a view
        children=move |counter: Counter| {
          view! {
            <button>"Value: " {move || counter.count.get()}</button>
          }
        }
      />
      <For
        // a function that returns the items we're iterating over; a signal is fine
        each=move || counters.get()
        // a unique key for each item
        key=|counter| counter.id
        // renders each item to a view
        children=move |counter: Counter| {
          view! {
            <button>"Value: " {move || counter.count.get()}</button>
          }
        }
      />
    </div>
  }
}

§Required Props

  • each: [IF]
    • Items over which the component should iterate.
  • key: [KF]
    • A key function that will be applied to each item.
  • children: [EF]
    • A function that takes the item, and returns the view that will be displayed for each item.

      §Syntax

      This can be passed directly in the view children of the <For/> by using the let: syntax to specify the name for the data variable passed in the argument.

      let (data, set_data) = create_signal(vec![0, 1, 2]);
      view! {
          <For
              each=move || data.get()
              key=|n| *n
              // stores the item in each row in a variable named `data`
              let:data
          >
              <p>{data}</p>
          </For>
      }

      is the same as

      let (data, set_data) = create_signal(vec![0, 1, 2]);
      view! {
         <For
             each=move || data.get()
             key=|n| *n
             children=|data| view! { <p>{data}</p> }
         />
      }

Fields§

§each: IF

Items over which the component should iterate.

§key: KF

A key function that will be applied to each item.

§children: EF

A function that takes the item, and returns the view that will be displayed for each item.

§Syntax

This can be passed directly in the view children of the <For/> by using the let: syntax to specify the name for the data variable passed in the argument.

let (data, set_data) = create_signal(vec![0, 1, 2]);
view! {
    <For
        each=move || data.get()
        key=|n| *n
        // stores the item in each row in a variable named `data`
        let:data
    >
        <p>{data}</p>
    </For>
}

is the same as

let (data, set_data) = create_signal(vec![0, 1, 2]);
view! {
   <For
       each=move || data.get()
       key=|n| *n
       children=|data| view! { <p>{data}</p> }
   />
}

Implementations§

source§

impl<IF, I, T, EF, N, KF, K> ForProps<IF, I, T, EF, N, KF, K>
where IF: Fn() -> I + 'static, I: IntoIterator<Item = T>, EF: Fn(T) -> N + 'static, N: IntoView + 'static, KF: Fn(&T) -> K + 'static, K: Eq + Hash + 'static, T: 'static,

source

pub fn builder() -> ForPropsBuilder<IF, I, T, EF, N, KF, K, ((), (), ())>

Create a builder for building ForProps. On the builder, call .each(...), .key(...), .children(...) to set the values of the fields. Finally, call .build() to create the instance of ForProps.

Trait Implementations§

source§

impl<IF, I, T, EF, N, KF, K> IntoView for ForProps<IF, I, T, EF, N, KF, K>
where IF: Fn() -> I + 'static, I: IntoIterator<Item = T>, EF: Fn(T) -> N + 'static, N: IntoView + 'static, KF: Fn(&T) -> K + 'static, K: Eq + Hash + 'static, T: 'static,

source§

fn into_view(self) -> View

Converts the value into View.

Auto Trait Implementations§

§

impl<IF, I, T, EF, N, KF, K> Freeze for ForProps<IF, I, T, EF, N, KF, K>
where IF: Freeze, KF: Freeze, EF: Freeze,

§

impl<IF, I, T, EF, N, KF, K> RefUnwindSafe for ForProps<IF, I, T, EF, N, KF, K>

§

impl<IF, I, T, EF, N, KF, K> Send for ForProps<IF, I, T, EF, N, KF, K>
where IF: Send, KF: Send, EF: Send,

§

impl<IF, I, T, EF, N, KF, K> Sync for ForProps<IF, I, T, EF, N, KF, K>
where IF: Sync, KF: Sync, EF: Sync,

§

impl<IF, I, T, EF, N, KF, K> Unpin for ForProps<IF, I, T, EF, N, KF, K>
where IF: Unpin, KF: Unpin, EF: Unpin,

§

impl<IF, I, T, EF, N, KF, K> UnwindSafe for ForProps<IF, I, T, EF, N, KF, K>
where IF: UnwindSafe, KF: UnwindSafe, EF: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more