props 改变后如何让组件在1s后才进行渲染呢??不多说,上代码
shouldComponentUpdate(nextProps: Readonly<{isLoading: boolean}>){
// 延迟1s关闭
if(this.props.isLoading == true && nextProps.isLoading == false){
setTimeout(() => {
this.setState({});
}, 1000);
return false;
}
return true;
}
虽然这里返回false但this.prop还是更新成nextProps,只是render没有被调用,所以界面渲染没有改变,但1秒后调用setState({}),shouldComponentUpdate返回true,render被调用,界面发生改变
本文介绍如何在React组件中使用shouldComponentUpdate方法,使得组件在接收到isLoading属性改变后延迟1秒再进行渲染,确保UI平滑过渡。
3911

被折叠的 条评论
为什么被折叠?



