liveBook · Manning

Component

As explained in Refactoring to Rust

A component is an isolated, reusable software unit that encapsulates a related portion of functionality, data, or user interface.

A component is an isolated package that separates concerns around related functionality, data, or user interface. Components make larger systems easier to organize and can be assembled into web applications. section 9.4

In a React interface, a component owns local state and returns markup. A list component can hold entries and a page number, render each entry, and respond to a More button without exposing its internal implementation. section 9.4

A stateful component commonly divides its responsibilities into:

  • State: stores values such as the current page, fetched entries, or a fetching status. section 9.5
  • Actions or messages: represent events such as incrementing a page, starting a search, or recording success and failure. section 9.5
  • View: converts the current state into user-interface output, including loading text, list entries, links, and controls. section 9.5

A list component across frameworks

The React version initializes entries, maps them into list items, and updates the page when the user clicks More. The Yew version models Fetching, Success, and Failed states; create initializes the component, update handles messages and asynchronous searches, and view renders the matching result. section 9.4 section 9.5

Listing 9.16 List.jsx: Creating a component with static data
import React, { useEffect, useState } from 'react'
const List = () => {
    const [entries, setEntries] = useState([{id:"abc", title:"title"}])  #1
    const [page, setPage] = useState(0)  #2

     return (
        <>
        <ul>
            {entries?.map((v, i) => {  #3
             return <li key={i}>
                <a href={`${v.id}`} target='_blank'>{v.title}</a>
             </li>
            })}
        </ul>
        <button onClick={() => setPage((page) => page + 1)}>More</button>  #4
        </>
    )
}
export default List;

Inspect the compact React implementation showing local state, mapped entries, links, and pagination. section 9.4

How Refactoring to Rust covers it

How deep it goes
Defines components through React examples and Yew's create, update, and view methods, while deferring deeper Properties usage.
Examples
Uses React and Yew list components with pagination, paper-search results, loading states, links, and asynchronous updates.
What you can do after
Build it from scratch with React or Yew, connect state transitions to rendering, and mount a WebAssembly component in an application.
How it connects
Connects components to React, Yew, WebAssembly, MVC, state management, actions, properties, and module bundling.

Also explained in

Compare how 10 books explain component →

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.