liveBook · Manning

State

As explained in Refactoring to Rust

State is data representing the current condition of a component or application.

State is data that records a component’s current condition. In a React list, it includes the displayed entries and current page; changing the page triggers a data fetch and updates the entries. In Yew, state can include both the current page and whether fetching succeeded, failed, or is underway. section 9.4 section 9.5

Component frameworks use state in different ways:

  • React uses state hooks and an effect to fetch entries when the page changes. section 9.4
  • Yew stores state on the component; create initializes it, update changes it in response to messages, and view renders from it. section 9.5
Listing 9.20 lib.rs: Creating enums for various states
use yew::prelude::*;  #1

...

pub enum Msg {  #2
    IncrementPage,
    SetFeedState(FetchState<Feed>),
    GetSearch(isize),
}

pub enum FetchState<T> {  #3
    Fetching,
    Success(T),
    Failed(reqwest::Error),
}

See a Yew example that represents fetch status as Fetching, Success, or Failed, and defines messages for component actions. section 9.5

How Refactoring to Rust covers it

How deep it goes
Defines state through working React and Yew examples, including initialization, updates, and rendering.
Examples
Uses a paginated article list and Yew fetch states for fetching, success, and failure.
What you can do after
Apply state through React hooks or Yew component methods to update and render a paginated article feed.
How it connects
Connects component state to React hooks, Yew messages, component rendering, and Model-View-Controller.

Also explained in

Compare how 39 books explain state →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.