1.用了hooks后生命周期函数放哪里
function App () {
useEffect( ()=>{
//等同于componetDidMount
} )
return () => {
//等同于componentDidWillUnmount
}
}
2.类实例成员变量映射到hooks
class App ...
... it = 0
function App ...
... const it = useRef(0)
3.hooks中获取历史props和state
用ref,因为ref不受重渲染影响
function App ...
const [a,setA] = useState(0);
const preARef = useRef();
useEffect( ()=>{
preARef.current = a;
} )
const preA = preARef.current;
return {a},{preA}
4.强制更新组件
function App ...
const [updater,setUpdater] = useState(0);
function forceUpdate(){
setUpdater(updater => updater+1)
}