nvm -v
npm -v
node -v
npm install -g yarn
npm install create-react-app -g
App.tsx
create-react-app --template typescript react-with-ts
cd react-with-ts && yarn start
- interface:useState<IState['brands']>([])
- React.FC
- Converting .jsx to .tsx: --Stackoverflow
-
creating multiple nested components
parent -> child1 -> child2 -> ... -> child5
-
child5 would apply the value of hook useContext as to render.
eng:https://devdocs.io/react/react-api#reactmemo
zh-cn:https://zh-hans.reactjs.org/docs/react-api.html#reactmemo
-
create a
index.js
file, then append a child calledSwatch
-
index.js is a React component,
- it has states:
count
,color
- two buttons can control its states and Re-Render App
Swatch
receive props includescolor
Swatch
without memo:
SWATCH HAS SAME PROPS: | click color button 5 times | ReRender App && Re-Render Swatch |
SWATCH HAS Different PROPS: | click color button 5 times | ReRender App && Re-Render Swatch |
Swatch
wrapped in memo:
const T = React.memo(Swatch)
return <T color={color}></T>
SWATCH HAS SAME PROPS: | click count button 5 times | ReRender App Only |
SWATCH HAS Different PROPS: | click color button 5 times | ReRender App && Re-Render Swatch |