React
GitbookFrontend2021-03-04
π SyntheticEvent
- if the interaction with
SyntheticEvent
is delayed (e.g. via setTimeout) it will be cleared and the.target.value
reference will no longer be valid.
const hanldeChange = (evt) => {
/**
* This is a simple implementation of a "debounce" function,
* which will queue an expression to be called in 250ms and
* cancel any pending queued expressions. This way we can
* delay the call 250ms after the user has stoped typing.
*/
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
this.setState({ search: evt.target.value });
}, 250);
};
π state
- global store: createContext/useContext + useReducer
- local (localStorage, sessionStorage)
- redux / mobx
π test
- jest
- mocha / chai
- selenium, cypress: a complete end-to-end testing experience.
π Async/Await vs Promise chain
-
Scope:
- in a
promise
, only the promise chain is asynchronous β doesnβt block the execution; - with
async/await
the entire wrapper function is asynchronous.
- in a
-
Logic:
- in a
promise
, synchronous work can be handled in the same callback, and multiple promises can be handled using Promise.all; - with
async/await
the synchronous work needs to be placed outside the callback, and multiple promises can be handled with more simple variables
- in a
-
Error handling:
- Promises:
then
, catch, finally; - Async/await:
try
, catch, finally
- Promises:
π mutable vs immutable
- Primitive data types such as
number
s,string
s, andBoolean
s areimmutable
object
s andarray
s are mutable
dependencies
- reacthook-form
- dayjs /date-fns
- bit.dev, material-ui
- react-router-dom
π Event loop/propagation
- web workers
- capturing, bubbling
- react.new
create-react-app
: remove serviceProvider- react-icons
- usehooks.com