react useRef与空依赖useMemo/useCallback
https://github.com/reactjs/reactjs.org/issues/2570
https://github.com/yaofly2012/note/issues/168
But even then, useMemo gives you the benefit of an expensive create function being called only once. If you want to use useRef with a create function, you’re forced to call this function on every render, that is via useRef(expensive()), which could perform very poorly.
初始化时,计算消耗不大的值(或不需要计算的值),优先使用useRef。
React对useMemo定位只是用于性能优化,并不保证在依赖项不变时,就一定不会重新执行函数。
所以要保证 useMemo 内的内容即使再执行,程序也能够正常运行!!
How to memoize calculations?里提到:
You may rely on useMemo as a performance optimization, not as a semantic guarantee.
Dan在twiter:A few small updates to Hooks FAQ based on common questions.里也强调:
useMemo() is a hint — not a guarantee. React may still choose to “forget” some memoized values to reclaim memory. Don’t rely on it for correctness
useMemo
会跟踪依赖项,并且通过比较依赖项是否发生变化决定是否重新执行函数。即使依赖项是个空数组,但是内部还是会做部分额外的逻辑。所以性能上比useRef
要差
forwardRef
返回值是一个渲染函数,可以添加 displayName
https://github.com/yaofly2012/note/issues/180
ts 变量后置 !
NoNullable 断言
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator
受控的思考
antd在设计表格受控时。
将所有的props都定义为初始状态生成用,后续的渲染都只会响应受控属性。
想要外部影响内部,只有确定传入受控属性才行,相当于用户确认接过处理责任。