pub fn create_multi_action<I, O, F, Fu>(
    cx: Scope,
    action_fn: F
) -> MultiAction<I, O>where
    I: 'static,
    O: 'static,
    F: Fn(&I) -> Fu + 'static,
    Fu: Future<Output = O> + 'static,
Expand description

Creates an MultiAction to synchronize an imperative async call to the synchronous reactive system.

If you’re trying to load data by running an async function reactively, you probably want to use a create_resource instead. If you’re trying to occasionally run an async function in response to something like a user clicking a button, you’re in the right place.

async fn send_new_todo_to_api(task: String) -> usize {
  // do something...
  // return a task id
  42
}
let add_todo = create_multi_action(cx, |task: &String| {
  // `task` is given as `&String` because its value is available in `input`
  send_new_todo_to_api(task.clone())
});

add_todo.dispatch("Buy milk".to_string());
add_todo.dispatch("???".to_string());
add_todo.dispatch("Profit!!!".to_string());

assert_eq!(add_todo.submissions().get().len(), 3);

The input to the async function should always be a single value, but it can be of any type. The argument is always passed by reference to the function, because it is stored in Submission::input as well.

// if there's a single argument, just use that
let action1 = create_multi_action(cx, |input: &String| {
    let input = input.clone();
    async move { todo!() }
});

// if there are no arguments, use the unit type `()`
let action2 = create_multi_action(cx, |input: &()| async { todo!() });

// if there are multiple arguments, use a tuple
let action3 =
    create_multi_action(cx, |input: &(usize, String)| async { todo!() });