Recap React - Accessibility / Code splitting
更新時間: 2021/11/20
Accessibility
- web accessibility (a11y)
- all
aria-*HTML attributes are fully supported in JSX (hyphen-cased) - every HTML form control needs to be labeled accessibly
<label htmlFor="namedInput">Name:</label> <input id="namedInput" type="text" name="name"/> - able to be
fully operatedwith keyboard - cases depending on
only pointer and mouse eventswill break functionality for keyboard users - setting the language
- setting the document title (describe the current page content)
- color contrast
- eslint-plugin-jsx-a11y
Code-splitting
- bundling - the process of
following imported filesandmergingthem intoa single file - create react app / Next.js / Gatsby uses
Webpack - create multiple bundles that can be
dynamically loaded at runtime(improve performance) - lazy-load only the things needed to improve performance
Dynamic import()
- when Webpack comes across dynamic import, it automatically starts
code-splittingth app
import("./math").then(math => {
console.log(math.add(16,26));
})
React.lazy
- render a dynamic import as a regular component
- takes a function that must call a dynamic
import()and return aPromisewhich resolves to a module with adefaultexport containing a React component - a lazy component should render inside a
Suspensecomponent (with fallback content, ex: loading) - wrap the lazy component with an error boundary to show a error message when modules fails to load
import React, { Suspense } from 'react';
import MyErrorBoundary from './MyErrorBoundary';
const OtherComponent = React.lazy(() => import('./OtherComponent'));
const AnotherComponent = React.lazy(() => import('./AnotherComponent'));
const MyComponent = () => (
<div>
<MyErrorBoundary>
<Suspense fallback={<div>Loading...</div>}>
<section>
<OtherComponent />
<AnotherComponent />
</section>
</Suspense>
</MyErrorBoundary>
</div>
);