module导入script方式
type="module"
1 |
|
type="module"
1 | <!DOCTYPE html> |
useMemo 是拿来保持一个对象引用不变的。useMemo 和 useCallback 都是 React 提供来做性能优化的。比起 classes, Hooks 给了开发者更高的灵活度和自由,但是对开发者要求也更高了,因为 Hooks 使用不恰当很容易导致性能问题。
假设有个 component,在 dataConfig 变化的时候重新去 fetchData:
1 | <Child |
如果是个 Class Component,会这么写:
1 | class Child extends React.Component<Props> { |
使用 Hooks 后长这样:
1 | const Child = ({ fetchData, dataConfig }: Props) => { |
使用 Class Component 时我们需要手动管理依赖,但是使用 Hooks 时会带来副作用:React 使用的是Object.is()
,如果fetchData
的 reference 变了,也会触发 useEffect
。
虽然逻辑上 React 的处理是合理的,但是还是需要手动去解决它导致的性能问题:官方提供了 useCallback 这个 hooks,用于解决函数引用问题。
1 | -Demo |
需命名为 webpack.config.js
1 | const path = require("path") // 模块 |
function has special rules:
Although each input will only have one output, but for different inputs may have the same output.
Given all these, pure functions have a big set of advantages. They are easier to read and understand, as they do one thing. There is no need to look outside the function code and check for variables. There is no need to think how changing the value of the variable will affect other functions. No mutations in other functions will affect the result of the pure function for a specific input.
Pure functions are easier to test, as all dependencies are in the function definition and they do one thing.
For Example:
1 | var arr = [1, 2, 3] |
Another Example:
1 | // Impure |