Code Splitting
import()
Before:
1 | import {add} from './math'; |
After:
1 | import("./math").then(math => { |
when i use create-react-app or next.js , i can operate it directly. 🗳
React.lazy()
Before:
1 | import OtherComponent from './OtherComponent'; |
After:
1 | const OtherComponent = React.lazy(() => import('./OtherComponent')); |
principle: when i use this .lazy api that must called import() grammar, it will return a promise as a default export containing a react component
question 💁♂️ : How to save components when they are not fully displayed ?
1 | const OtherComponent = React.lazy(() => import('./OtherComponent')); |
Yep ! that’s a Suspense
brocade bag(👛) saved us.
Fragments
Context
before:
1 | class App extends React.Component { |
After:
1 | const ThemeContext = React.createContext('light'); |
Forwarding refs to DOM components
1 | const FancyButton = React.forwardRef((props, ref) => ( |
Fancy Button get a ref to proxy button element
procedure 👨🏼🔧:
1.create a react ref by calling React.createRef
2.pass to <FancyButton ref={ref}>
3.React passes the (props, ref) => …function inside
forwardRef as a second argument
4.forward this ref
argument down to <button ref={ref}>
5.ref is attached
when i use HOC, that’s also gonna be work by React.forwardRef
Optimizing Performance
Brunch
1 | yarn add --dev terser-brunch |
Then: 🦉
1 | brunch build -p |
Browserify
1 | yarn add --dev envify terser uglifyify |
Rollup
1 | yarn add --dev rollup-plugin-commonjs rollup-plugin-replace rollup-plugin-terser |
Next Lesson we clap for Hooks
! 👏🏻
若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏
扫描二维码,分享此文章