Skip to content

Latest commit

 

History

History
247 lines (130 loc) · 3.2 KB

react-intro-5.md

File metadata and controls

247 lines (130 loc) · 3.2 KB
marp theme html
true
ctheme
true

width:600px


ui is a state


Types of state

  • URL state
  • Server state
  • Form state
  • Local state
  • App state

URL anatomy

width:900px


URL state

width:700px


URL state

Tools that simplify work with URLs are often referred as "routers"


URL state

 <BrowserRouter>
    <Routes>
      <Route path="/" element={<App />}>
        <Route index element={<Home />} />
        <Route path="teams" element={<Teams />}>
          <Route path=":teamId" element={<Team />} />
          <Route path="new" element={<NewTeamForm />} />

URL state

  <Route path="users/:userID" element={<UserProfile />} />
import { useParams } from 'react-router-dom';

function ProfilePage() {
  // Get the userID param from the URL.
  let { userID } = useParams();
  // ...
}

URL state

import { useSearchParams, useNavigate } from "react-router-dom";

function App() {
  let [searchParams, setSearchParams] = useSearchParams();
  let navigate = useNavigate(); // --> navigate("/posts")
  ...

Form state

width:900px


Form state

width:450px


Form state

Keeps track of useful metadata for fields and form such as isDirty, dirtyFields, touchedFields, isSubmitted, isSubmitSuccessful, isSubmitting, isValid, errors, ...


Form state


Server state

Keeps track of useful metadata describing communication with BE resource: isLoading, error, ...

Also can include caching, normalization, optimistic updates and other advanced techniques


Server state

const { status, data, error, isFetching } = usePosts();

Server state

const getPosts = async () => {
  const { data } = await axios.get(
    "https://jsonplaceholder.typicode.com/posts"
  );
  return data;
};

export default function usePosts() {
  return useQuery("posts", getPosts);
}

Server state


App state

Some data in time that affects the whole application


App state


App state

width:800px


Resources

<style scoped>ul li { font-size: 0.8rem; }</style>