当前位置: 首页 > news >正文

wordpress建站哪里好设计企业网站多少钱

wordpress建站哪里好,设计企业网站多少钱,欧美设计网站推荐,简述创建一个网站的过程文章目录 捕获错误 hook创建错误边界组件 Provider定义错误边界组件定义边界组件状态捕捉错误渲染备份组件重置组件通过 useHook 控制边界组件 捕获错误 hook getDerivedStateFromError 返回值会作为组件的 state 用于展示错误时的内容 componentDidCatch 创建错误边界组件 P… 文章目录 捕获错误 hook创建错误边界组件 Provider定义错误边界组件定义边界组件状态捕捉错误渲染备份组件重置组件通过 useHook 控制边界组件 捕获错误 hook getDerivedStateFromError 返回值会作为组件的 state 用于展示错误时的内容 componentDidCatch 创建错误边界组件 Provider 错误边界组件其实是一个通过 Context.Provider 包裹的组件这样使得组件内部可以获取到捕捉的相关操作 import { createContext } from react;export type ErrorBoundaryContextType {didCatch: boolean;error: any;resetErrorBoundary: (...args: any[]) void; };// 错误边界组件其实是一个通过 Context.Provider 包裹的组件 export const ErrorBoundaryContext createContextErrorBoundaryContextType | null(null);定义错误边界组件 定义边界组件状态 type ErrorBoundaryState | {didCatch: true;error: any;}| {didCatch: false;error: null;};const initialState: ErrorBoundaryState {didCatch: false, // 错误是否捕捉error: null, // 捕捉到的错误信息 };捕捉错误 getDerivedStateFromError 捕捉到错误后设置组件状态展示备份组件componentDidCatch 用于触发错误回调 export class ErrorBoundary extends ComponentErrorBoundaryProps,ErrorBoundaryState{constructor(props: ErrorBoundaryProps) {super(props);this.resetErrorBoundary this.resetErrorBoundary.bind(this);this.state initialState;}static getDerivedStateFromError(error: Error) {return { didCatch: true, error };}componentDidCatch(error: Error, info: ErrorInfo) {this.props.onError?.(error, info);}}渲染备份组件 通过指定的参数名区分是无状态组件还是有状态组件 无状态组件通过直接调用函数传递 props有状态组件通过 createElement 传递 props 通过 createElement 处理传递的组件更加优雅 createElement(元素类型,参数,子元素)详情其中第一个参数可以直接传递 Context.Provider export class ErrorBoundary extends ComponentErrorBoundaryProps,ErrorBoundaryState{// ...render() {const { children, fallbackRender, FallbackComponent, fallback } this.props;const { didCatch, error } this.state;let childToRender children;// 如果捕捉到了错误if (didCatch) {const props: FallbackProps {error,resetErrorBoundary: this.resetErrorBoundary,};// 通过指定的参数名区分是无状态组件还是有状态组件if (typeof fallbackRender function) {childToRender fallbackRender(props);} else if (FallbackComponent) {childToRender createElement(FallbackComponent, props);} else if (fallback null || isValidElement(fallback)) {childToRender fallback;} else {if (isDevelopment) {console.error(react-error-boundary requires either a fallback, fallbackRender, or FallbackComponent prop);}throw error;}}// Context.Provider 可以直接作为 createElement 的第一个参数return createElement(ErrorBoundaryContext.Provider,{value: { // Context.Provider 提供可供消费的内容didCatch,error,resetErrorBoundary: this.resetErrorBoundary,},},childToRender);}// ... }重置组件 将错误信息重置使得能渲染原组件 const initialState: ErrorBoundaryState {didCatch: false, // 错误是否捕捉error: null, // 捕捉到的错误信息 };export class ErrorBoundary extends ComponentErrorBoundaryProps,ErrorBoundaryState{// ...resetErrorBoundary(...args: any[]) {const { error } this.state;if (error ! null) {this.props.onReset?.({ // 触发对应回调args,reason: imperative-api,});this.setState(initialState);}}// ...// 根据 resetKeys 重置但并未对外暴露该 APIcomponentDidUpdate(prevProps: ErrorBoundaryProps,prevState: ErrorBoundaryState) {const { didCatch } this.state;const { resetKeys } this.props;// Theres an edge case where if the thing that triggered the error happens to *also* be in the resetKeys array,// wed end up resetting the error boundary immediately.// This would likely trigger a second error to be thrown.// So we make sure that we dont check the resetKeys on the first call of cDU after the error is set.if (didCatch prevState.error ! null hasArrayChanged(prevProps.resetKeys, resetKeys)) {this.props.onReset?.({next: resetKeys,prev: prevProps.resetKeys,reason: keys,});this.setState(initialState);}} }function hasArrayChanged(a: any[] [], b: any[] []) {return (a.length ! b.length || a.some((item, index) !Object.is(item, b[index]))); }通过 useHook 控制边界组件 通过 context 获取最近的边界组件内容通过手动抛出错误重新触发边界组件 import { useContext, useMemo, useState } from react; import { assertErrorBoundaryContext } from ./assertErrorBoundaryContext; import { ErrorBoundaryContext } from ./ErrorBoundaryContext;type UseErrorBoundaryStateTError | { error: TError; hasError: true }| { error: null; hasError: false };export type UseErrorBoundaryApiTError {resetBoundary: () void;showBoundary: (error: TError) void; };export function useErrorBoundaryTError any(): UseErrorBoundaryApiTError {// 获取最近的边界组件 Provider 的内容const context useContext(ErrorBoundaryContext);// 断言 Context 是否为空assertErrorBoundaryContext(context);const [state, setState] useStateUseErrorBoundaryStateTError({error: null,hasError: false,});const memoized useMemo(() ({resetBoundary: () {// 提供 Provider 对应的重置边界组件方法渲染原组件context.resetErrorBoundary();setState({ error: null, hasError: false });},// 手动抛出错误触发边界组件showBoundary: (error: TError) setState({error,hasError: true,}),}),[context.resetErrorBoundary]);// 当调用 showBoundary 后该 hook 会手动抛出错误让边界组件来捕捉if (state.hasError) {throw state.error;}return memoized; }
http://www.pierceye.com/news/786198/

相关文章:

  • 做网站要不要钱网站如何做触屏滑动
  • 加工企业网站这么做常德网站建设企业
  • 百度举报网站wordpress主题缩略图
  • 南宁快速网站建设电话WordPress电影公司网站
  • 什么网站可以直接做word文档亚马逊周末可以视频认证吗
  • 网站设计申请书买购网官方网站
  • 深圳做网站建设公司青岛网景互联网站建设公司
  • 做公司网站要多少钱洛阳信息网
  • asp 网站名字免费的公众号排版工具
  • 郑州响应式建站查企业的信息在哪个官网
  • 大型企业网站开发怎么使用免费的wordpress
  • 大连做网站大公司建设项目咨询公司网站
  • 教育培训网站建设方案鞍山建设信息网站
  • 重庆网站建设哪家强平台如何做推广
  • 安徽省建设安全监督站的网站网站建设公司一般多少钱
  • 服装网站建设策划书3000字软件开发包含网站开发吗
  • 免费网站的建设绵阳网站建设制作
  • 学生处网站建设招标公告网站包括哪些主要内容
  • 成都门户网站建设多少钱聚合广告联盟
  • 坦克大战网站开发课程设计报告软文营销的本质
  • 美食网站开发网站登录验证码是怎么做的
  • 电子商务网站排名辽宁省建设工程信息网业绩公示
  • 天津建设科技杂志的官方网站wordpress cnzz插件
  • 滨州建设网站太原网站建设优化
  • 记事本做网站怎么改字体包装设计模板设计素材
  • 下载软件的网站推荐thinkphp和wordpress
  • 青海省城乡和住房建设厅网站合肥小吃培训网页设计
  • 财经门户网站建设django校园网站开发
  • 泉州网站建设报价广东建设厅网站
  • 建设网站的源代码的所有权wordpress网站打开慢