译者简介 zqlu 蚂蚁金服·数据体验技术团队

翻译自Ultimate React Component Patterns with Typescript 2.8,作者Martin Hochel

这篇博客受React Component Patterns启发而写

在线Demo

有状态组件、无状态组件、默认属性、Render回调、组件注入、泛型组件、高阶组件、受控组件

如果你了解我,你就已经知道我不编写没有类型定义的javascript代码,所以我从0.9版本后,就非常喜欢TypeScript了。除了有类型的JS,我也非常喜欢React库,所以当把React和Typescript 结合在一起后,对我来说就像置身天堂一样:)。整个应用程序和虚拟DOM中的完整的类型安全,是非常奇妙和开心的。

所以这篇文章说是关于什么的呢?在互联网上有各种关于React组件模式的文章,但没有介绍如何将这些模式应用到Typescript中。此外,即将发布的TS 2.8版本带来了另人兴奋的新功能如、如有条件的类型(conditional types)、标准库中新预定义的条件类型、同态映射类型修饰符等等,这些新功能是我们能够以类型安全的方式轻松地创建常见的React组件模式。

这篇文章篇幅会比较长,所以请你坐下放轻松,与此同时你将掌握Typescript下的 终极React组件模式。

所有的模式/例子均使用typescript 2.8版本和strict mode

准备开始

首先,我们需要安装typescript和tslibs帮助程序库,以便我们生出的代码更小

yarn add -D typescript@next	
# tslib 将仅用与您的编译目标不支持的功能	
yarn add tslib

有了这个,我们可以初始化我们的typescript配置:

# 这条命令将在我们的工程中创建默认配置 tsconfig.json	
yarn tsc --init

现在我们来安装 react、react-dom 和它们的类型定义。

yarn add react react-dom	
yarn add -D @types/{react,react-dom}

棒极啦!现在我们可以开始进入我们的组件模式吧,不是吗?

无状态组件

你猜到了,这些是没有state的组件(也被称为展示型组件)。在部分时候,它们也是纯函数组件。让我们用TypeScript创建人造的无状态Button组件。

同使用原生JS一样,我们需要引入React以便我们可以使用JSX

import React from 'react'	
const Button = ({ onClick: handleClick, children }) => (	<button onClick={handleClick}>{children}</button>	
)

虽然 tsc 编译器现在还会跑出错误!我们需要显式的告诉我们的组件/函数我们的props是什么类型的。让我们定义我们的 props:

import React, { MouseEvent, ReactNode } from 'react'	
type Props = { 	onClick(e: MouseEvent<HTMLElement>): void	children?: ReactNode 	
}	
const Button = ({ onClick: handleClick, children }: Props) => (	<button onClick={handleClick}>{children}</button>	
)

现在我们已经解决了所有的错误了!非常好!但我们还可以做的更好!

在 @types/react中已经预定义一个类型 type SFC<P>,它也是类型 interfaceStatelessComponent<P>的一个别名,此外,它已经有预定义的 children和其他(defaultProps、displayName等等…),所以我们不用每次都自己编写!

所以最后的无状态组件是这样的:

import React, { MouseEvent, SFC } from 'react';	
type Props = { onClick(e: MouseEvent<HTMLElement>): void };	
const Button: SFC<Props> = ({ onClick: handleClick, children }) => (	<button onClick={handleClick}>{children}</button>	
);

有状态组件

让我们使用我们的Button组件来创建有状态的计数器组件。

首先我们需要定义 initialState

const initialState = { clicksCount: 0 }

现在我们将使用TypeScript来从我们的实现中推断出State的类型。

这样我们不需要分开维护我们的类型定义和实现,我们只有唯一的真相源,即我们的实现,太好了!

type State = Readonly<typeof initialState>

另外请注意,该类型被明确映射为使所有的属性均为只读的。我们需要再次使用State类型来显式地在我们的class上定义只读的state属性。

readonly state: State = initialState

这么做的作用是什么?

我们知道我们在React中不能像下面这样直接更新 state

this.state.clicksCount = 2;	
this.state = { clicksCount: 2 }

这将导致运行时错误,但在编译时不会报错。通过显式地使用 Readonly映射我们的 typeState,和在我们的类定义中设置只读的state属性,TS将会让我们立刻知道我们做错了。

整个容器组件/有状态组件的实现:

我们的容器组件还没有任何Props API,所以我们需要将 Compoent组件的第一个泛型参数定义为 Object(因为在React中 props永远是对象 {}),并使用 State类型作为第二个泛型参数。

import React, { Component } from 'react';	
import Button from './Button';	
const initialState = { clicksCount: 0 };	
type State = Readonly<typeof initialState>;	
class ButtonCounter extends Component<object, State> {	readonly state: State = initialState;	render() {	const { clicksCount } = this.state;	return (	<>	<Button onClick={this.handleIncrement}>Increment</Button>	<Button onClick={this.handleDecrement}>Decrement</Button>	You've clicked me {clicksCount} times!	</>	);	}	private handleIncrement = () => this.setState(incrementClicksCount);	private handleDecrement = () => this.setState(decrementClicksCount);	
}	
const incrementClicksCount = (prevState: State) => ({	clicksCount: prevState.clicksCount + 1,	
});	
const decrementClicksCount = (prevState: State) => ({	clicksCount: prevState.clicksCount - 1,	
});

你可能已经注意到了我们将状态更新函数提取到类的外部作为纯函数。这是一种常见的模式,这样我们不需要了解渲染逻辑就可以简单的测试这些状态更新函数。此外,因为我们使用了TypeScript并将State显式地映射为只读的,它将阻止我们在这些函数中做一些更改状态的操作:

const decrementClicksCount = (prevState: State) => ({	clicksCount: prevState.clicksCount--,	
});	
// 这样讲抛出编译错误:	
//	
// [ts] Cannot assign to 'clicksCount' because it is a constant or a read-only property.

非常酷是吧?:)


默认属性

让我们扩展我们的Button组件,新增一个string类型的颜色属性。

type Props = {	onClick(e: MouseEvent<HTMLElement>): void;	color: string;	
};

如果我们想定义默认属性,我们可以在我们的组件中通过 Button.defaultProps={…}来定义。

通过这样做,我们需要改变我们的属性类型定义来标记属性是可选有默认值的。

所以定义是这样的(注意 ?操作符)

type Props = {	onClick(e: MouseEvent<HTMLElement>): void;	color?: string;	
};

此时我们的组件实现是这样的:

const Button: SFC<Props> = ({ onClick: handleClick, color, children }) => (	<button style={{ color }} onClick={handleClick}>	{children}	</button>	
);

尽管这样在我们简单的例子中可用的,这有一个问题。因为我们在strict mode模式洗啊,可选的属性 color的类型是一个联合类型 undefined|string

比如我们想对color属性做一些操作,TS将会抛出一个错误,因为它并不知道它在React创建中通过 Component.defaultProps中已经定义了。

为了满足TS编译器,我们可以使用下面3种技术:

  • 使用!操作符在render函数显式地告诉编译器这个变量不会是 undefined,尽管它是可选的,如: <buttononClick={handleClick!}>{children}</button>

  • 使用条件语句/三目运算符来让编译器明白一些属性是没有被定义的: <buttononClick={handleClick ?handleClick:undefined}>{children}</button>

  • 创建可服用的 withDefaultProps高阶函数,它将更新我们的props类型定义和设置默认属性。我认为这是最简洁干净的方案。

我们可以很简单的实现我们的高阶函数(感谢TS 2.8种的条件类型映射):

export const withDefaultProps = <	P extends object,	DP extends Partial<P> = Partial<P>	
>(	defaultProps: DP,	Cmp: ComponentType<P>,	
) => {	// 提取出必须的属性	type RequiredProps = Omit<P, keyof DP>;	// 重新创建我们的属性定义,通过一个相交类型,将所有的原始属性标记成可选的,必选的属性标记成可选的	type Props = Partial<DP> & Required<RequiredProps>;	Cmp.defaultProps = defaultProps;	// 返回重新的定义的属性类型组件,通过将原始组件的类型检查关闭,然后再设置正确的属性类型	return (Cmp as ComponentType<any>) as ComponentType<Props>;	
};

现在我们可以使用 withDefaultProps高阶函数来定义我们的默认属性,同时也解决了之前的问题:

const defaultProps = {	color: 'red',	
};	
type DefaultProps = typeof defaultProps;	
type Props = { onClick(e: MouseEvent<HTMLElement>): void } & DefaultProps;	
const Button: SFC<Props> = ({ onClick: handleClick, color, children }) => (	<button style={{ color }} onClick={handleClick}>	{children}	</button>	
);	
const ButtonWithDefaultProps = withDefaultProps(defaultProps, Button);

或者直接使用内联(注意我们需要显式的提供原始Button组件的属性定义,TS不能从函数中推断出参数的类型):

const ButtonWithDefaultProps = withDefaultProps<Props>(	defaultProps,	({ onClick: handleClick, color, children }) => (	<button style={{ color }} onClick={handleClick}>	{children}	</button>	),	
);

现在Button组件的属性已经被正确的定义被使用的,默认属性被反应出来并且在类型定义中是可选的,但在实现中是必选的!

{	onClick(e: MouseEvent<HTMLElement>): void	color?: string	
}

640?wx_fmt=png

组件使用方法仍然是一样的:

render() {	return (	<ButtonWithDefaultProps	onClick={this.handleIncrement}	>	Increment	</ButtonWithDefaultProps>	)	
}

当然这也使用与通过 class定义的组件(得益于TS中的类结构起源,我们不需要显式指定我们的 Props泛型类型)。

它看起来像这样:

const ButtonViaClass = withDefaultProps(	defaultProps,	class Button extends Component<Props> {	render() {	const { onClick: handleClick, color, children } = this.props;	return (	<button style={{ color }} onClick={handleClick}>	{Children}	</button>	);	}	},	
);

再次,它的使用方式仍然是一样的:

render() {	return (	<ButtonViaClass onClick={this.handleIncrement}>Increment</ButtonViaClass>	);	
}

比如说你需要构建一个可展开的菜单组件,它需要在用户点击它时显示子内容。我们就可以使用各种各样的组件模式来实现它。

render回调/render属性模式

实现组件的逻辑可复用的最好方式将组件的children放到函数中去,或者利用 render属性API——这也是为什么Render回调也被称为函数子组件。

让我们用render属性方法实现一个 Toggleable组件:

import React, { Component, MouseEvent } from 'react';	
import { isFunction } from '../utils';	
const initialState = {	show: false,	
};	
type State = Readonly<typeof initialState>;	
type Props = Partial<{	children: RenderCallback;	render: RenderCallback;	
}>;	
type RenderCallback = (args: ToggleableComponentProps) => JSX.Element;	
type ToggleableComponentProps = {	show: State['show'];	toggle: Toggleable['toggle'];	
};	
export class Toggleable extends Component<Props, State> {	readonly state: State = initialState;	render() {	const { render, children } = this.props;	const renderProps = {	show: this.state.show,	toggle: this.toggle,	};	if (render) {	return render(renderProps);	}	return isFunction(children) ? children(renderProps) : null;	}	private toggle = (event: MouseEvent<HTMLElement>) =>	this.setState(updateShowState);	
}	
const updateShowState = (prevState: State) => ({ show: !prevState.show });

这里都发生了什么,让我们来分别看看重要的部分:

const initialState = {	show: false,	
};	
type State = Readonly<typeof initialState>;
  • 这里我们和前面的例子一样声明了我们的state

现在我们来定义组件的props(注意这里我们使用了Partitial映射类型,因为我们所有的属性都是可选的,不用分别对每个属性手动添加 ?标识符):

type Props = Partial<{	children: RenderCallback;	render: RenderCallback;	
}>;	
type RenderCallback = (args: ToggleableComponentProps) => JSX.Element;	
type ToggleableComponentProps = {	show: State['show'];	toggle: Toggleable['toggle'];	
};

我们需要同时支持child作为函数,和render属性作为函数,它们两者都是可选的。为了避免重复代码,我们定义了 RenderCallback作为我们的渲染函数定义:

type RenderCallback = (args: ToggleableComponentProps) => JSX.Element

在读者眼中看起来比较奇怪的部分是我们最后的别名类型: typeToggleableComponentProps

type ToggleableComponentProps = {	show: State['show'];	toggle: Toggleable['toggle'];	
};

这里我们使用了TypeScript的查找类型(lookup types),所以我们又不需要重复地去定义类型了:

  • show:State['show']我们利用已有的state类型定义了 show属性

  • toggle:Toggleable['toggle']我们利用了TS从类实现推断类类型来定义 toggle属性。很好用而且非常强大。

剩下的实现部分很简单,标准的render属性/children作为函数的模式:

export class Toggleable extends Component<Props, State> {	// ...	render() {	const { render, children } = this.props;	const renderProps = {	show: this.state.show,	toggle: this.toggle,	};	if (render) {	return render(renderProps);	}	return isFunction(children) ? children(renderProps) : null;	}	// ...	
}

现在我们可以把函数作为children传给Toggleable组件了:

<Toggleable>	{({ show, toggle }) => (	<>	<div onClick={toggle}>	<h1>some title</h1>	</div>	{show ? <p>some content</p> : null}	</>	)}	
</Toggleable>

或者我们可以把函数作为render属性:

<Toggleable	render={({ show, toggle }) => (	<>	<div onClick={toggle}>	<h1>some title</h1>	</div>	{show ? <p>some content</p> : null}	</>	)}	
/>

感谢TypeScript,我们在render属性的参数有了智能提示和正确的类型检查。

如果我们想复用它(比如用在多个菜单组件中),我们只需要创建一个使用Toggleable逻辑的心组件:

type Props = { title: string }	
const ToggleableMenu: SFC<Props> = ({ title, children }) => (	<Toggleable	render={({ show, toggle }) => (	<>	<div onClick={toggle}>	<h1>title</h1>	</div>	{show ? children : null}	</>	)}	/>	
)

现在我们全新的 ToggleableMenu组件已经可以在Menu组件中使用了:

export class Menu extends Component {	render() {	return (	<>	<ToggleableMenu title="First Menu">Some content</ToggleableMenu>	<ToggleableMenu title="Second Menu">Some content</ToggleableMenu>	<ToggleableMenu title="Third Menu">Some content</ToggleableMenu>	</>	);	}	
}

并且它也像我们期望的那样工作了

这中模式在我们想更改渲染的内容,而不关心状态改变的情况下非常有用:可以看到,我们将渲染逻辑移到ToggleableMenu组件的额children函数中了,但把状态管理逻辑保留在我们的Toggleable组件中!

组件注入

为了让我们的组件更灵活,我们可以引入组件注入模式。

什么是组件注入模式呢?如果你对React-Router比较熟悉,那你已经在下面这样路由定义的时候使用这种模式了:

<Route path="/foo" component={MyView} />

这样我们不是把函数传递给render/children属性,而是通过 component属性“注入”组件。为此,我们可以重构,把我们的内置render属性函数改成一个可复用的无状态组件:

type MenuItemProps = { title: string };	
const MenuItem: SFC<MenuItemProps & ToggleableComponentProps> = ({	title,	toggle,	show,	children,	
}) => (	<>	<div onClick={toggle}>	<h1>{title}</h1>	</div>	{show ? children : null}	</>	
);

有了这个,我们可以使用render属性重构 ToggleableMenu

type Props = { title: string };	
const ToggleableMenu: SFC<Props> = ({ title, children }) => (	<Toggleable	render={({ show, toggle }) => (	<MenuItem show={show} toggle={toggle} title={title}>	{children}	</MenuItem>	)}	/>	
);

这个完成之后,让我们来开始定义我们新的API—— compoent属性。

我们需要更新我们的属性API。

  • children现在可以是函数或者ReactNode(当component属性被使用时)

  • component是我们新的API,它可以接受实现了 ToggleableComponentProps属性的组件,并且它需要是设置为any的泛型,这样各种各样的实现组件可以添加其他属性到 ToggleableComponentProps并通过TS的验证

  • props我们引入可以传入任意属性的定义。它被定义成any类型的可索引类型,这里我们放松了严格的类型安全检查...

// 我们需要使用我们任意的props类型来创建 defaultProps,默认是一个空对象	
const defaultProps = { props: {} as { [name: string]: any } };	
type Props = Partial<	{	children: RenderCallback | ReactNode;	render: RenderCallback;	component: ComponentType<ToggleableComponentProps<any>>;	} & DefaultProps	
>;	
type DefaultProps = typeof defaultProps;

下一步,我们需要添加新的属性API到 ToggleableComponentProps上,以便用户可以通过 <Toggleableprops={...}/>来使用我们的 props属性:

export type ToggleableComponentProps<P extends object = object> = {	show: State['show'];	toggle: Toggleable['toggle'];	
} & P;

然后我们需要更新我们的 render函数:

  render() {	const {	component: InjectedComponent,	props,	render,	children,	} = this.props;	const renderProps = {	show: this.state.show,	toggle: this.toggle,	};	// 当 component 属性被使用时,children 是 ReactNode 而不是函数	if (InjectedComponent) {	return (	<InjectedComponent {...props} {...renderProps}>	{children}	</InjectedComponent>	);	}	if (render) {	return render(renderProps);	}	return isFunction(children) ? children(renderProps) : null;	}

完整的Toggleable组件实现如下,支持 render 属性、children作为函数、组件注入功能:

import React, { Component, ReactNode, ComponentType, MouseEvent } from 'react';	
import { isFunction, getHocComponentName } from '../utils';	
const initialState = { show: false };	
const defaultProps = { props: {} as { [name: string]: any } };	
type State = Readonly<typeof initialState>;	
type Props = Partial<	{	children: RenderCallback | ReactNode;	render: RenderCallback;	component: ComponentType<ToggleableComponentProps<any>>;	} & DefaultProps	
>;	
type DefaultProps = typeof defaultProps;	
type RenderCallback = (args: ToggleableComponentProps) => JSX.Element;	
export type ToggleableComponentProps<P extends object = object> = {	show: State['show'];	toggle: Toggleable['toggle'];	
} & P;	
export class Toggleable extends Component<Props, State> {	static readonly defaultProps: Props = defaultProps;	readonly state: State = initialState;	render() {	const {	component: InjectedComponent,	props,	render,	children,	} = this.props;	const renderProps = {	show: this.state.show,	toggle: this.toggle,	};	if (InjectedComponent) {	return (	<InjectedComponent {...props} {...renderProps}>	{children}	</InjectedComponent>	);	}	if (render) {	return render(renderProps);	}	return isFunction(children) ? children(renderProps) : null;	}	private toggle = (event: MouseEvent<HTMLElement>) =>	this.setState(updateShowState);	
}	
const updateShowState = (prevState: State) => ({ show: !prevState.show });

我们最终使用 component属性的 ToggleableMenuViaComponentInjection组件是这样的:

const ToggleableMenuViaComponentInjection: SFC<ToggleableMenuProps> = ({	title,	children,	
}) => (	<Toggleable component={MenuItem} props={{ title }}>	{children}	</Toggleable>	
);

请注意,这里我们的 props属性没有严格的类型安全检查,因为它被定义成索引对象类型 {[name:string]:any}:

我们可以还是像之前一样使用 ToggleableMenuViaComponentInjection组件来实现菜单渲染:

export class Menu extends Component {	render() {	return (	<>	<ToggleableMenuViaComponentInjection title="First Menu">	Some content	</ToggleableMenuViaComponentInjection>	<ToggleableMenuViaComponentInjection title="Second Menu">	Another content	</ToggleableMenuViaComponentInjection>	<ToggleableMenuViaComponentInjection title="Third Menu">	More content	</ToggleableMenuViaComponentInjection>	</>	);	}	
}

泛型组件

在我们视线“组件注入模式”的时候,我们失去了对 props属性严格的类型安全检查。我们怎样修复这个问题呢?对,你猜到了!我们可以把我们的 Toggleable组件实现为一个泛型组件!

首先我们需要把我们的属性泛型化。我们使用默认的泛型参数,所以我们不需要在没必要的时候显式地提供类型(针对 render 属性和 children 作为函数)。

type Props<P extends object = object> = Partial<	{	children: RenderCallback | ReactNode;	render: RenderCallback;	component: ComponentType<ToggleableComponentProps<P>>;	} & DefaultProps<P>	
>;

我们也需要把 ToggleableComponnetProps更新成泛型的。不,等等,它已经是泛型啦!所以还不需要做任何更改。

需要更新的是 typeDefaultProps,因为不支持从声明实现推倒出泛型类型定义,所以需要把它重构成传统的类型定义->实现:

type DefaultProps<P extends object = object> = { props: P };	
const defaultProps: DefaultProps = { props: {} };

就快好啦!

现在让我们把组件类也泛型化。再次说明,我们使用了默认的属性,所以在没有使用组件注入的时候不需要去指定泛型参数!

export class Toggleable<T = {}> extends Component<Props<T>, State> {}

这样就完成了吗?嗯…,我们可以在JSX中使用泛型类型吗?

坏消息是,不能...

但我们可以在泛型组件上引入 ofType的工场模式:

export class Toggleable<T = {}> extends Component<Props<T>, State> {	static ofType<T extends object>() {	return Toggleable as Constructor<Toggleable<T>>;	}	
}

完整的 Toggleable 组件实现,支持 Render 属性、Children 作为函数、带泛型 props 属性支持的组件注入:

import React, {	Component,	ReactNode,	ComponentType,	MouseEvent,	SFC,	
} from 'react';	
import { isFunction, getHocComponentName } from '../utils';	
const initialState = { show: false };	
// const defaultProps = { props: {} as { [name: string]: any } };	
type State = Readonly<typeof initialState>;	
type Props<P extends object = object> = Partial<	{	children: RenderCallback | ReactNode;	render: RenderCallback;	component: ComponentType<ToggleableComponentProps<P>>;	} & DefaultProps<P>	
>;	
type DefaultProps<P extends object = object> = { props: P };	
const defaultProps: DefaultProps = { props: {} };	
type RenderCallback = (args: ToggleableComponentProps) => JSX.Element;	
export type ToggleableComponentProps<P extends object = object> = {	show: State['show'];	toggle: Toggleable['toggle'];	
} & P;	
export class Toggleable<T = {}> extends Component<Props<T>, State> {	static ofType<T extends object>() {	return Toggleable as Constructor<Toggleable<T>>;	}	static readonly defaultProps: Props = defaultProps;	readonly state: State = initialState;	render() {	const {	component: InjectedComponent,	props,	render,	children,	} = this.props;	const renderProps = {	show: this.state.show,	toggle: this.toggle,	};	if (InjectedComponent) {	return (	<InjectedComponent {...props} {...renderProps}>	{children}	</InjectedComponent>	);	}	if (render) {	return render(renderProps);	}	return isFunction(children) ? children(renderProps) : null;	}	private toggle = (event: MouseEvent<HTMLElement>) =>	this.setState(updateShowState);	
}	
const updateShowState = (prevState: State) => ({ show: !prevState.show });

有了 staticofType工厂函数后,我们可以创建正确类型的泛型组件了。

type MenuItemProps = { title: string };	
// ofType 是一种标识函数,返回的是相同实现的 Toggleable 组件,但带有制定的 props 类型	
const ToggleableWithTitle = Toggleable.ofType<MenuItemProps>();	
type ToggleableMenuProps = MenuItemProps;	
const ToggleableMenuViaComponentInjection: SFC<ToggleableMenuProps> = ({	title,	children,	
}) => (	<ToggleableWithTitle component={MenuItem} props={{ title }}>	{children}	</ToggleableWithTitle>	
);

并且所有的东西都还像一起一样工作,但这次我有的 props={} 属性有了正确的类型检查。鼓掌吧!

高阶组件

因为我们已经创建了带render回调功能的 Toggleable组件,实现HOC也会很容易。(这也是 render 回调函数模式的一个大优势,因为我们可以使用HOC来实现)

让我们开始实现我们的HOC组件吧:

我们需要创建:

  • displayName (以便我们在devtools可以很好地调试)

  • WrappedComponent (以便我们能够获取原始的组件——对测试很有用)

  • 使用 hoist-non-react-staticsnpm包中的 hoistNonReactStatics

import React, { ComponentType, Component } from 'react';	
import hoistNonReactStatics from 'hoist-non-react-statics';	
import { getHocComponentName } from '../utils';	
import {	Toggleable,	Props as ToggleableProps,	ToggleableComponentProps,	
} from './RenderProps';	
// OwnProps 是内部组件上任意公开的属性	
type OwnProps = object;	
type InjectedProps = ToggleableComponentProps;	
export const withToggleable = <OriginalProps extends object>(	UnwrappedComponent: ComponentType<OriginalProps & InjectedProps>,	
) => {	// 我们使用 TS 2.8 中的条件映射类型来得到我们最终的属性类型	type Props = Omit<OriginalProps, keyof InjectedProps> & OwnProps;	class WithToggleable extends Component<Props> {	static readonly displayName = getHocComponentName(	WithToggleable.displayName,	UnwrappedComponent,	);	static readonly UnwrappedComponent = UnwrappedComponent;	render() {	const { ...rest } = this.props;	return (	<Toggleable	render={renderProps => (	<UnwrappedComponent {...rest} {...renderProps} />	)}	/>	);	}	}	return hoistNonReactStatics(WithToggleable, UnwrappedComponent);	
};

现在我们可以使用HOC来创建我们的 Toggleable菜单组件了,并有正确的类型安全检查!

const ToggleableMenuViaHOC = withToggleable(MenuItem)

一切正常,还有类型安全检查!好极了!

受控组件

这是最后一个组件模式了!假设我们想从父组件中控制我们的 Toggleable组件,我们需要 Toggleable组件配置化。这是一种很强大的模式。让我们来实现它吧。

当我说受控组件时,我指的是什么?我想从 Menu组件内控制所以的 ToggleableManu组件的内容是否显示。

我们需要像这样更新我们的 ToggleableMenu组件的实现:

// 更新我们的属性类型,以便我们可以通过 show 属性来控制是否显示	
type Props = MenuItemProps & { show?: boolean };	
// 注意:这里我们使用了结构来创建变量别,为了不和 render 回调函数的 show 参数冲突	
// -> { show: showContent }	
// Render 属性	
export const ToggleMenu: SFC<ToggleableComponentProps> = ({	title,	children,	show: showContent,	
}) => (	<Toggleable show={showContent}>	{({ show, toggle }) => (	<MenuItem title={title} toggle={toggle} show={show}>	{children}	</MenuItem>	)}	</Toggleable>	
);	
// 组件注入	
const ToggleableWithTitle = Toggleable.ofType<MenuItemProps>();	
export const ToggleableMenuViaComponentInjection: SFC<Props> = ({	title,	children,	show: showContent,	
}) => (	<ToggleableWithTitle	component={MenuItem}	props={{ title }}	show={showContent}	>	{children}	</ToggleableWithTitle>	
);	
// HOC不需要更改	
export const ToggleMenuViaHOC = withToggleable(MenuItem);

有了这些更新后,我们可以在 Menu中添加状态,并传递给 ToggleableMenu

const initialState = { showContents: false };	
type State = Readonly<typeof initialState>;	
export class Menu extends Component<object, State> {	readonly state: State = initialState;	render() {	const { showContents } = this.state;	return (	<>	<button onClick={this.toggleShowContents}>toggle showContent</button>	<hr />	<ToggleableMenu title="First Menu" show={showContents}>	Some Content	</ToggleableMenu>	<ToggleableMenu title="Second Menu" show={showContents}>	Another Content	</ToggleableMenu>	<ToggleableMenu title="Third Menu" show={showContents}>	More Content	</ToggleableMenu>	</>	);	}	
}

让我们为了最终的功能和灵活性最后一次更新 Toggleable组件。为了让 Toggleable 变成受控组件我们需要:

  1. 添加 show属性到 PropsAPI上

  2. 更新默认的属性(因为show是可选的)

  3. 从Props.show更新组件的初始化state,因为现在我们状态中值可能取决于父组件传来的属性

  4. 在componentWillReceiveProps生命周期函数中从props更新state

1 & 2

const initialState = { show: false }	
const defaultProps: DefaultProps = { ...initialState, props: {} }	
type State = Readonly<typeof initialState>	
type DefaultProps<P extends object = object> = { props: P } & Pick<State, 'show'>

3 & 4

export class Toggleable<T = {}> extends Component<Props<T>, State> {	static readonly defaultProps: Props = defaultProps	// Bang operator used, I know I know ...	state: State = { show: this.props.show! }	componentWillReceiveProps(nextProps: Props<T>) {	const currentProps = this.props	if (nextProps.show !== currentProps.show) {	this.setState({ show: Boolean(nextProps.show) })	}	}	
}

最终支持所有所有模式(Render属性/Children作为函数/组件注入/泛型组件/受控组件)的 Toggleable 组件:

import React, { Component, MouseEvent, ComponentType, ReactNode } from 'react'	
import { isFunction, getHocComponentName } from '../utils'	
const initialState = { show: false }	
const defaultProps: DefaultProps = { ...initialState, props: {} }	
type State = Readonly<typeof initialState>	
export type Props<P extends object = object> = Partial<	{	children: RenderCallback | ReactNode	render: RenderCallback	component: ComponentType<ToggleableComponentProps<P>>	} & DefaultProps<P>	
>	
type RenderCallback = (args: ToggleableComponentProps) => JSX.Element	
export type ToggleableComponentProps<P extends object = object> = {	show: State['show']	toggle: Toggleable['toggle']	
} & P	
type DefaultProps<P extends object = object> = { props: P } & Pick<State, 'show'>	
export class Toggleable<T extends object = object> extends Component<Props<T>, State> {	static ofType<T extends object>() {	return Toggleable as Constructor<Toggleable<T>>	}	static readonly defaultProps: Props = defaultProps	readonly state: State = { show: this.props.show! }	componentWillReceiveProps(nextProps: Props<T>, nextContext: any) {	const currentProps = this.props	if (nextProps.show !== currentProps.show) {	this.setState({ show: Boolean(nextProps.show) })	}	}	render() {	const { component: InjectedComponent, children, render, props } = this.props	const renderProps = { show: this.state.show, toggle: this.toggle }	if (InjectedComponent) {	return (	<InjectedComponent {...props} {...renderProps}>	{children}	</InjectedComponent>	)	}	if (render) {	return render(renderProps)	}	return isFunction(children) ? children(renderProps) : new Error('asdsa()')	}	private toggle = (event: MouseEvent<HTMLElement>) => this.setState(updateShowState)	
}	
const updateShowState = (prevState: State) => ({ show: !prevState.show })

最终的Toggleable HOC 组件 withToggleable

只需要稍作修改 -> 我们需要在HOC组件中传递 show 属性,并更新我们的 OwnPropsAPI

import React, { ComponentType, Component } from 'react'	
import hoistNonReactStatics from 'hoist-non-react-statics'	
import { getHocComponentName } from '../utils'	
import {	Toggleable,	Props as ToggleableProps,	ToggleableComponentProps as InjectedProps,	
} from './toggleable'	
// OwnProps is for any public props that should be available on internal Component.props	
// and for WrappedComponent	
type OwnProps = Pick<ToggleableProps, 'show'>	
export const withToogleable = <OriginalProps extends object>(	UnwrappedComponent: ComponentType<OriginalProps & InjectedProps>	
) => {	// we are leveraging TS 2.8 conditional mapped types to get proper final prop types	type Props = Omit<OriginalProps, keyof InjectedProps> & OwnProps	class WithToggleable extends Component<Props> {	static readonly displayName = getHocComponentName(	WithToggleable.displayName,	UnwrappedComponent	)	static readonly WrappedComponent = UnwrappedComponent	render() {	// Generics and spread issue	// https://github.com/Microsoft/TypeScript/issues/10727	const { show, ...rest } = this.props as Pick<Props, 'show'> // we need to explicitly pick props we wanna destructure, rest is gonna be type `{}`	return (	<Toggleable	show={show}	render={renderProps => <UnwrappedComponent {...rest} {...renderProps} />}	/>	)	}	}	return hoistNonReactStatics(WithToggleable, UnwrappedComponent as any) as ComponentType<Props>	
}

总结

使用 TypeScript 和 React 时,实现恰当的类型安全组件可能会很棘手。但随着 TypeScript 2.8中新加入的功能,我们几乎可以在所有的 React 组件模式中编写类型安全的组件。

在这遍非常长(对此十分抱歉)文章中,感谢TypeScript,我们已经学会了在各种各样的模式下怎么编写严格类型安全检查的组件。

在这些模式中最强的应该是Render属性模式,它让我们可以在此基础上不需要太多改动就可以实现其他常见的模式,如组件注入、高阶组件等。

文中所有的demo都可以在我的 Github 仓库中找到。

此外,需要明白的是,本文中演示的模版类型安全,只能在使用 VDOM/JSX 的库中实现。

  • Angular 模版有 Language service 提供类型安全,但像 ngFor 等简单的构造检查好像都不行...

  • Vue 的模版不像 Angular,它们的模版和数据绑定只是神奇的字符串(但这有可能在未来会改变。尽管你可以在模版中使用VDOM,但因为各种类型的属性定义,它使用起来十分笨重(这怪 snabdom...))

和往常一样,如果你有任何问题,可以在这或者 twitter(@martin_hotell)联系我,另外,快乐的类型检查伙伴们,干杯!

查看全文
如若内容造成侵权/违法违规/事实不符,请联系编程学习网邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

相关文章

  1. webpack插件编写_如何使用Webpack和渐进式Web技术编写简单的现代JavaScript应用程序...

    webpack插件编写by Anurag Majumdar通过阿努拉格马朱达尔 如何使用Webpack和渐进式Web技术编写简单的现代JavaScript应用程序 (How to write simple modern JavaScript apps with Webpack and progressive web techniques) Have you thought about making modern JavaScript a…...

    2024/4/21 12:29:56
  2. 轻松的节点身份验证:将所有帐户链接在一起

    This will be the final article in our Easy Node Authentication Series. We will be using all of the previous articles together. 这将是我们的Easy Node Authentication Series中的最后一篇文章。 我们将一起使用所有先前的文章。 Edit 11/18/2017: Updated to reflect…...

    2024/4/21 12:29:55
  3. 如何使用Entity Framework Core和Highcharts创建费用管理器

    介绍 (Introduction) In this article, we will be creating a personal expense manager using ASP.NET Core 2.1 and Entity Framework (EF) core Code First approach. This expense manager tracks your daily expenses and provides comparative charts to show your expe…...

    2024/4/21 12:29:54
  4. 你们这些阿猫

    缘起 考完 Final 又是一个 Spring Break&#xff0c;10 天很无聊啊&#xff0c;人一无聊就想写代码&#xff0c;但是前面写守望 UI CSS 的时候写伤了&#xff0c;而且 Spring Break 就 10 天&#xff0c;实在不想写一个大项目。 有一天听到了古巨基的《爱与诚》里面唱的&#x…...

    2024/5/3 9:50:02
  5. React+Redux打造“NEWS EARLY”单页应用 一步步让你理解最前沿技术栈的真谛

    之前写过一篇文章&#xff0c;分享了我利用闲暇时间&#xff0c;使用ReactRedux技术栈重构的百度某产品个人中心页面。您可以参考这里&#xff0c;或者参考Github代码仓库地址。 这个工程实例中&#xff0c;我采用了厂内的工程构建工具&#xff0d;FIS&#xff0c;并贯穿了reac…...

    2024/4/21 12:29:52
  6. React+Redux 打造 “NEWS EARLY” 单页应用 一个项目理解最前沿技术栈真谛

    之前写过一篇文章&#xff0c;分享了我利用闲暇时间&#xff0c;使用ReactRedux技术栈重构的百度某产品个人中心页面。您可以参考这里&#xff0c;或者参考Github代码仓库地址。这个工程实例中&#xff0c;我采用了厂内的工程构建工具&#xff0d;FIS3&#xff0c;并贯穿了reac…...

    2024/4/20 21:03:39
  7. 基于SSM框架大型分布式电商系统开发(13-14)

    前言 消息中间件解决方案JMSSpringBoot框架与短信解决方案 因为是根据大佬的项目点滴做起&#xff0c;如果看到此博客侵犯利益&#xff0c;请告知立即删除。 第13章 消息中间件解决方案JMS 1.JMS入门 1.1 消息中间件 1.1.1 品优购系统模块调用关系分析 我们已经完成了5个w…...

    2024/4/21 12:29:50
  8. 双眼皮恢复慢体质

    ...

    2024/5/2 6:06:27
  9. 埋线双眼皮11豪

    ...

    2024/4/21 12:29:48
  10. 一、Webstrom+React+Ant Design+echarts搭建react项目

    前言 一、React是Facebook推出的一个前端框架&#xff0c;之前被用于著名的社交媒体Instagram中&#xff0c;后来由于取得了不错的反响&#xff0c;于是Facebook决定将其开源。出身名门的React也不负众望&#xff0c;成功成为当前最火热的三大前端框架之一。相比于Angular&…...

    2024/4/21 12:29:47
  11. 使用 Vue + ElementUI + Webpack + VueRouter 做后台管理、RESTful 交互

    一、前言 1、前端三大 JS 框架 Vue、React、Angular 都用了一段时间了&#xff0c;最后还是回归于 Vue JSdemoVue[增删改查] 使用 Vue2.x LayUI 做后台管理 CRUD 界面和 REST 交互React [增删改查] 使用 React LayUI 做后台管理 CRUD 界面和 RESTful 交互Angular 使用 Angul…...

    2024/4/21 12:29:47
  12. 双眼皮线细

    ...

    2024/5/6 6:02:15
  13. 眼皮松弛凹陷可以做眼皮松弛适合什么眼皮松弛做全切双眼皮有用吗

    ...

    2024/4/21 12:29:45
  14. 双眼皮修复一个月后可以吗

    ...

    2024/4/20 16:25:32
  15. 双眼皮二十天可以洗头吗

    ...

    2024/5/2 17:28:17
  16. 双眼皮20天可以洗头吗

    ...

    2024/4/20 16:25:31
  17. 割双眼皮人家说我眼睛脂肪多

    ...

    2024/5/2 7:39:22
  18. 1个月内2次双眼皮7天注意事项

    ...

    2024/4/21 12:29:43
  19. 2016 年谷歌开源了超酷炫的项目

    开放源代码软件让 Google 能够无需重新发明轮子就能够快速有效地进行开发&#xff0c;也让我们能够集中注意力来解决新问题。我们知道&#xff0c;支持开源&#xff0c;就是站在了巨人的肩膀上&#xff0c;所以 Google 员工能够轻松地将他们在内部工作的项目作为开放源代码发布…...

    2024/4/21 12:29:42
  20. 直接拿来用!最火前端开源项目(二)

    摘要&#xff1a;如今开源项目的火热程度已无需再多言语&#xff0c;在&#xff08;一&#xff09;中为开发者整理了九大类的开源项目列表&#xff0c;开发者们&#xff0c;你们用的怎么样了&#xff1f;本文继续整理GitHub上最火的前端开源项目列表&#xff0c;列出十个分类&a…...

    2024/4/21 12:29:41

最新文章

  1. 柯桥西语培训之在西班牙旅游点菜哪些坑不能踩?

    Por muy bien que se coma en Espaa —que es mucho— hay una cosa innegable: lo que pasa en la cocina se queda en la cocina. No todos los alimentos son igualmente seguros o sabrosos cuando se encuentran fuera de la comodidad de nuestra propia casa. Ya sea po…...

    2024/5/6 20:38:41
  2. 梯度消失和梯度爆炸的一些处理方法

    在这里是记录一下梯度消失或梯度爆炸的一些处理技巧。全当学习总结了如有错误还请留言&#xff0c;在此感激不尽。 权重和梯度的更新公式如下&#xff1a; w w − η ⋅ ∇ w w w - \eta \cdot \nabla w ww−η⋅∇w 个人通俗的理解梯度消失就是网络模型在反向求导的时候出…...

    2024/5/6 9:38:23
  3. Chrome 浏览器无法保存或自动填充密码

    Chrome 浏览器无法保存或自动填充密码 分类 平时使用 Chrome 浏览器都会对网站的用户名密码自动填充&#xff0c;今天发现突然不行了&#xff0c;找到一个解决办法&#xff1a; 1、退出 Chrome 浏览器。2、打开 Chrome 安装目录下的的 Profile 目录&#xff0c;删除 Login Da…...

    2024/5/6 12:09:41
  4. 数据挖掘|贝叶斯分类器及其Python实现

    分类分析|贝叶斯分类器及其Python实现 0. 分类分析概述1. Logistics回归模型2. 贝叶斯分类器2.1 贝叶斯定理2.2 朴素贝叶斯分类器2.2.1 高斯朴素贝叶斯分类器2.2.2 多项式朴素贝叶斯分类器 2.3 朴素贝叶斯分类的主要优点2.4 朴素贝叶斯分类的主要缺点 3. 贝叶斯分类器在生产中的…...

    2024/5/5 19:53:23
  5. 【外汇早评】美通胀数据走低,美元调整

    原标题:【外汇早评】美通胀数据走低,美元调整昨日美国方面公布了新一期的核心PCE物价指数数据,同比增长1.6%,低于前值和预期值的1.7%,距离美联储的通胀目标2%继续走低,通胀压力较低,且此前美国一季度GDP初值中的消费部分下滑明显,因此市场对美联储后续更可能降息的政策…...

    2024/5/4 23:54:56
  6. 【原油贵金属周评】原油多头拥挤,价格调整

    原标题:【原油贵金属周评】原油多头拥挤,价格调整本周国际劳动节,我们喜迎四天假期,但是整个金融市场确实流动性充沛,大事频发,各个商品波动剧烈。美国方面,在本周四凌晨公布5月份的利率决议和新闻发布会,维持联邦基金利率在2.25%-2.50%不变,符合市场预期。同时美联储…...

    2024/5/4 23:54:56
  7. 【外汇周评】靓丽非农不及疲软通胀影响

    原标题:【外汇周评】靓丽非农不及疲软通胀影响在刚结束的周五,美国方面公布了新一期的非农就业数据,大幅好于前值和预期,新增就业重新回到20万以上。具体数据: 美国4月非农就业人口变动 26.3万人,预期 19万人,前值 19.6万人。 美国4月失业率 3.6%,预期 3.8%,前值 3…...

    2024/5/4 23:54:56
  8. 【原油贵金属早评】库存继续增加,油价收跌

    原标题:【原油贵金属早评】库存继续增加,油价收跌周三清晨公布美国当周API原油库存数据,上周原油库存增加281万桶至4.692亿桶,增幅超过预期的74.4万桶。且有消息人士称,沙特阿美据悉将于6月向亚洲炼油厂额外出售更多原油,印度炼油商预计将每日获得至多20万桶的额外原油供…...

    2024/5/6 9:21:00
  9. 【外汇早评】日本央行会议纪要不改日元强势

    原标题:【外汇早评】日本央行会议纪要不改日元强势近两日日元大幅走强与近期市场风险情绪上升,避险资金回流日元有关,也与前一段时间的美日贸易谈判给日本缓冲期,日本方面对汇率问题也避免继续贬值有关。虽然今日早间日本央行公布的利率会议纪要仍然是支持宽松政策,但这符…...

    2024/5/4 23:54:56
  10. 【原油贵金属早评】欧佩克稳定市场,填补伊朗问题的影响

    原标题:【原油贵金属早评】欧佩克稳定市场,填补伊朗问题的影响近日伊朗局势升温,导致市场担忧影响原油供给,油价试图反弹。此时OPEC表态稳定市场。据消息人士透露,沙特6月石油出口料将低于700万桶/日,沙特已经收到石油消费国提出的6月份扩大出口的“适度要求”,沙特将满…...

    2024/5/4 23:55:05
  11. 【外汇早评】美欲与伊朗重谈协议

    原标题:【外汇早评】美欲与伊朗重谈协议美国对伊朗的制裁遭到伊朗的抗议,昨日伊朗方面提出将部分退出伊核协议。而此行为又遭到欧洲方面对伊朗的谴责和警告,伊朗外长昨日回应称,欧洲国家履行它们的义务,伊核协议就能保证存续。据传闻伊朗的导弹已经对准了以色列和美国的航…...

    2024/5/4 23:54:56
  12. 【原油贵金属早评】波动率飙升,市场情绪动荡

    原标题:【原油贵金属早评】波动率飙升,市场情绪动荡因中美贸易谈判不安情绪影响,金融市场各资产品种出现明显的波动。随着美国与中方开启第十一轮谈判之际,美国按照既定计划向中国2000亿商品征收25%的关税,市场情绪有所平复,已经开始接受这一事实。虽然波动率-恐慌指数VI…...

    2024/5/4 23:55:16
  13. 【原油贵金属周评】伊朗局势升温,黄金多头跃跃欲试

    原标题:【原油贵金属周评】伊朗局势升温,黄金多头跃跃欲试美国和伊朗的局势继续升温,市场风险情绪上升,避险黄金有向上突破阻力的迹象。原油方面稍显平稳,近期美国和OPEC加大供给及市场需求回落的影响,伊朗局势并未推升油价走强。近期中美贸易谈判摩擦再度升级,美国对中…...

    2024/5/4 23:54:56
  14. 【原油贵金属早评】市场情绪继续恶化,黄金上破

    原标题:【原油贵金属早评】市场情绪继续恶化,黄金上破周初中国针对于美国加征关税的进行的反制措施引发市场情绪的大幅波动,人民币汇率出现大幅的贬值动能,金融市场受到非常明显的冲击。尤其是波动率起来之后,对于股市的表现尤其不安。隔夜美国股市出现明显的下行走势,这…...

    2024/5/6 1:40:42
  15. 【外汇早评】美伊僵持,风险情绪继续升温

    原标题:【外汇早评】美伊僵持,风险情绪继续升温昨日沙特两艘油轮再次发生爆炸事件,导致波斯湾局势进一步恶化,市场担忧美伊可能会出现摩擦生火,避险品种获得支撑,黄金和日元大幅走强。美指受中美贸易问题影响而在低位震荡。继5月12日,四艘商船在阿联酋领海附近的阿曼湾、…...

    2024/5/4 23:54:56
  16. 【原油贵金属早评】贸易冲突导致需求低迷,油价弱势

    原标题:【原油贵金属早评】贸易冲突导致需求低迷,油价弱势近日虽然伊朗局势升温,中东地区几起油船被袭击事件影响,但油价并未走高,而是出于调整结构中。由于市场预期局势失控的可能性较低,而中美贸易问题导致的全球经济衰退风险更大,需求会持续低迷,因此油价调整压力较…...

    2024/5/4 23:55:17
  17. 氧生福地 玩美北湖(上)——为时光守候两千年

    原标题:氧生福地 玩美北湖(上)——为时光守候两千年一次说走就走的旅行,只有一张高铁票的距离~ 所以,湖南郴州,我来了~ 从广州南站出发,一个半小时就到达郴州西站了。在动车上,同时改票的南风兄和我居然被分到了一个车厢,所以一路非常愉快地聊了过来。 挺好,最起…...

    2024/5/4 23:55:06
  18. 氧生福地 玩美北湖(中)——永春梯田里的美与鲜

    原标题:氧生福地 玩美北湖(中)——永春梯田里的美与鲜一觉醒来,因为大家太爱“美”照,在柳毅山庄去寻找龙女而错过了早餐时间。近十点,向导坏坏还是带着饥肠辘辘的我们去吃郴州最富有盛名的“鱼头粉”。说这是“十二分推荐”,到郴州必吃的美食之一。 哇塞!那个味美香甜…...

    2024/5/4 23:54:56
  19. 氧生福地 玩美北湖(下)——奔跑吧骚年!

    原标题:氧生福地 玩美北湖(下)——奔跑吧骚年!让我们红尘做伴 活得潇潇洒洒 策马奔腾共享人世繁华 对酒当歌唱出心中喜悦 轰轰烈烈把握青春年华 让我们红尘做伴 活得潇潇洒洒 策马奔腾共享人世繁华 对酒当歌唱出心中喜悦 轰轰烈烈把握青春年华 啊……啊……啊 两…...

    2024/5/4 23:55:06
  20. 扒开伪装医用面膜,翻六倍价格宰客,小姐姐注意了!

    原标题:扒开伪装医用面膜,翻六倍价格宰客,小姐姐注意了!扒开伪装医用面膜,翻六倍价格宰客!当行业里的某一品项火爆了,就会有很多商家蹭热度,装逼忽悠,最近火爆朋友圈的医用面膜,被沾上了污点,到底怎么回事呢? “比普通面膜安全、效果好!痘痘、痘印、敏感肌都能用…...

    2024/5/5 8:13:33
  21. 「发现」铁皮石斛仙草之神奇功效用于医用面膜

    原标题:「发现」铁皮石斛仙草之神奇功效用于医用面膜丽彦妆铁皮石斛医用面膜|石斛多糖无菌修护补水贴19大优势: 1、铁皮石斛:自唐宋以来,一直被列为皇室贡品,铁皮石斛生于海拔1600米的悬崖峭壁之上,繁殖力差,产量极低,所以古代仅供皇室、贵族享用 2、铁皮石斛自古民间…...

    2024/5/4 23:55:16
  22. 丽彦妆\医用面膜\冷敷贴轻奢医学护肤引导者

    原标题:丽彦妆\医用面膜\冷敷贴轻奢医学护肤引导者【公司简介】 广州华彬企业隶属香港华彬集团有限公司,专注美业21年,其旗下品牌: 「圣茵美」私密荷尔蒙抗衰,产后修复 「圣仪轩」私密荷尔蒙抗衰,产后修复 「花茵莳」私密荷尔蒙抗衰,产后修复 「丽彦妆」专注医学护…...

    2024/5/4 23:54:58
  23. 广州械字号面膜生产厂家OEM/ODM4项须知!

    原标题:广州械字号面膜生产厂家OEM/ODM4项须知!广州械字号面膜生产厂家OEM/ODM流程及注意事项解读: 械字号医用面膜,其实在我国并没有严格的定义,通常我们说的医美面膜指的应该是一种「医用敷料」,也就是说,医用面膜其实算作「医疗器械」的一种,又称「医用冷敷贴」。 …...

    2024/5/4 23:55:01
  24. 械字号医用眼膜缓解用眼过度到底有无作用?

    原标题:械字号医用眼膜缓解用眼过度到底有无作用?医用眼膜/械字号眼膜/医用冷敷眼贴 凝胶层为亲水高分子材料,含70%以上的水分。体表皮肤温度传导到本产品的凝胶层,热量被凝胶内水分子吸收,通过水分的蒸发带走大量的热量,可迅速地降低体表皮肤局部温度,减轻局部皮肤的灼…...

    2024/5/4 23:54:56
  25. 配置失败还原请勿关闭计算机,电脑开机屏幕上面显示,配置失败还原更改 请勿关闭计算机 开不了机 这个问题怎么办...

    解析如下&#xff1a;1、长按电脑电源键直至关机&#xff0c;然后再按一次电源健重启电脑&#xff0c;按F8健进入安全模式2、安全模式下进入Windows系统桌面后&#xff0c;按住“winR”打开运行窗口&#xff0c;输入“services.msc”打开服务设置3、在服务界面&#xff0c;选中…...

    2022/11/19 21:17:18
  26. 错误使用 reshape要执行 RESHAPE,请勿更改元素数目。

    %读入6幅图像&#xff08;每一幅图像的大小是564*564&#xff09; f1 imread(WashingtonDC_Band1_564.tif); subplot(3,2,1),imshow(f1); f2 imread(WashingtonDC_Band2_564.tif); subplot(3,2,2),imshow(f2); f3 imread(WashingtonDC_Band3_564.tif); subplot(3,2,3),imsho…...

    2022/11/19 21:17:16
  27. 配置 已完成 请勿关闭计算机,win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机...

    win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机”问题的解决方法在win7系统关机时如果有升级系统的或者其他需要会直接进入一个 等待界面&#xff0c;在等待界面中我们需要等待操作结束才能关机&#xff0c;虽然这比较麻烦&#xff0c;但是对系统进行配置和升级…...

    2022/11/19 21:17:15
  28. 台式电脑显示配置100%请勿关闭计算机,“准备配置windows 请勿关闭计算机”的解决方法...

    有不少用户在重装Win7系统或更新系统后会遇到“准备配置windows&#xff0c;请勿关闭计算机”的提示&#xff0c;要过很久才能进入系统&#xff0c;有的用户甚至几个小时也无法进入&#xff0c;下面就教大家这个问题的解决方法。第一种方法&#xff1a;我们首先在左下角的“开始…...

    2022/11/19 21:17:14
  29. win7 正在配置 请勿关闭计算机,怎么办Win7开机显示正在配置Windows Update请勿关机...

    置信有很多用户都跟小编一样遇到过这样的问题&#xff0c;电脑时发现开机屏幕显现“正在配置Windows Update&#xff0c;请勿关机”(如下图所示)&#xff0c;而且还需求等大约5分钟才干进入系统。这是怎样回事呢&#xff1f;一切都是正常操作的&#xff0c;为什么开时机呈现“正…...

    2022/11/19 21:17:13
  30. 准备配置windows 请勿关闭计算机 蓝屏,Win7开机总是出现提示“配置Windows请勿关机”...

    Win7系统开机启动时总是出现“配置Windows请勿关机”的提示&#xff0c;没过几秒后电脑自动重启&#xff0c;每次开机都这样无法进入系统&#xff0c;此时碰到这种现象的用户就可以使用以下5种方法解决问题。方法一&#xff1a;开机按下F8&#xff0c;在出现的Windows高级启动选…...

    2022/11/19 21:17:12
  31. 准备windows请勿关闭计算机要多久,windows10系统提示正在准备windows请勿关闭计算机怎么办...

    有不少windows10系统用户反映说碰到这样一个情况&#xff0c;就是电脑提示正在准备windows请勿关闭计算机&#xff0c;碰到这样的问题该怎么解决呢&#xff0c;现在小编就给大家分享一下windows10系统提示正在准备windows请勿关闭计算机的具体第一种方法&#xff1a;1、2、依次…...

    2022/11/19 21:17:11
  32. 配置 已完成 请勿关闭计算机,win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机”的解决方法...

    今天和大家分享一下win7系统重装了Win7旗舰版系统后&#xff0c;每次关机的时候桌面上都会显示一个“配置Windows Update的界面&#xff0c;提示请勿关闭计算机”&#xff0c;每次停留好几分钟才能正常关机&#xff0c;导致什么情况引起的呢&#xff1f;出现配置Windows Update…...

    2022/11/19 21:17:10
  33. 电脑桌面一直是清理请关闭计算机,windows7一直卡在清理 请勿关闭计算机-win7清理请勿关机,win7配置更新35%不动...

    只能是等着&#xff0c;别无他法。说是卡着如果你看硬盘灯应该在读写。如果从 Win 10 无法正常回滚&#xff0c;只能是考虑备份数据后重装系统了。解决来方案一&#xff1a;管理员运行cmd&#xff1a;net stop WuAuServcd %windir%ren SoftwareDistribution SDoldnet start WuA…...

    2022/11/19 21:17:09
  34. 计算机配置更新不起,电脑提示“配置Windows Update请勿关闭计算机”怎么办?

    原标题&#xff1a;电脑提示“配置Windows Update请勿关闭计算机”怎么办&#xff1f;win7系统中在开机与关闭的时候总是显示“配置windows update请勿关闭计算机”相信有不少朋友都曾遇到过一次两次还能忍但经常遇到就叫人感到心烦了遇到这种问题怎么办呢&#xff1f;一般的方…...

    2022/11/19 21:17:08
  35. 计算机正在配置无法关机,关机提示 windows7 正在配置windows 请勿关闭计算机 ,然后等了一晚上也没有关掉。现在电脑无法正常关机...

    关机提示 windows7 正在配置windows 请勿关闭计算机 &#xff0c;然后等了一晚上也没有关掉。现在电脑无法正常关机以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容&#xff0c;让我们赶快一起来看一下吧&#xff01;关机提示 windows7 正在配…...

    2022/11/19 21:17:05
  36. 钉钉提示请勿通过开发者调试模式_钉钉请勿通过开发者调试模式是真的吗好不好用...

    钉钉请勿通过开发者调试模式是真的吗好不好用 更新时间:2020-04-20 22:24:19 浏览次数:729次 区域: 南阳 > 卧龙 列举网提醒您:为保障您的权益,请不要提前支付任何费用! 虚拟位置外设器!!轨迹模拟&虚拟位置外设神器 专业用于:钉钉,外勤365,红圈通,企业微信和…...

    2022/11/19 21:17:05
  37. 配置失败还原请勿关闭计算机怎么办,win7系统出现“配置windows update失败 还原更改 请勿关闭计算机”,长时间没反应,无法进入系统的解决方案...

    前几天班里有位学生电脑(windows 7系统)出问题了&#xff0c;具体表现是开机时一直停留在“配置windows update失败 还原更改 请勿关闭计算机”这个界面&#xff0c;长时间没反应&#xff0c;无法进入系统。这个问题原来帮其他同学也解决过&#xff0c;网上搜了不少资料&#x…...

    2022/11/19 21:17:04
  38. 一个电脑无法关闭计算机你应该怎么办,电脑显示“清理请勿关闭计算机”怎么办?...

    本文为你提供了3个有效解决电脑显示“清理请勿关闭计算机”问题的方法&#xff0c;并在最后教给你1种保护系统安全的好方法&#xff0c;一起来看看&#xff01;电脑出现“清理请勿关闭计算机”在Windows 7(SP1)和Windows Server 2008 R2 SP1中&#xff0c;添加了1个新功能在“磁…...

    2022/11/19 21:17:03
  39. 请勿关闭计算机还原更改要多久,电脑显示:配置windows更新失败,正在还原更改,请勿关闭计算机怎么办...

    许多用户在长期不使用电脑的时候&#xff0c;开启电脑发现电脑显示&#xff1a;配置windows更新失败&#xff0c;正在还原更改&#xff0c;请勿关闭计算机。。.这要怎么办呢&#xff1f;下面小编就带着大家一起看看吧&#xff01;如果能够正常进入系统&#xff0c;建议您暂时移…...

    2022/11/19 21:17:02
  40. 还原更改请勿关闭计算机 要多久,配置windows update失败 还原更改 请勿关闭计算机,电脑开机后一直显示以...

    配置windows update失败 还原更改 请勿关闭计算机&#xff0c;电脑开机后一直显示以以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容&#xff0c;让我们赶快一起来看一下吧&#xff01;配置windows update失败 还原更改 请勿关闭计算机&#x…...

    2022/11/19 21:17:01
  41. 电脑配置中请勿关闭计算机怎么办,准备配置windows请勿关闭计算机一直显示怎么办【图解】...

    不知道大家有没有遇到过这样的一个问题&#xff0c;就是我们的win7系统在关机的时候&#xff0c;总是喜欢显示“准备配置windows&#xff0c;请勿关机”这样的一个页面&#xff0c;没有什么大碍&#xff0c;但是如果一直等着的话就要两个小时甚至更久都关不了机&#xff0c;非常…...

    2022/11/19 21:17:00
  42. 正在准备配置请勿关闭计算机,正在准备配置windows请勿关闭计算机时间长了解决教程...

    当电脑出现正在准备配置windows请勿关闭计算机时&#xff0c;一般是您正对windows进行升级&#xff0c;但是这个要是长时间没有反应&#xff0c;我们不能再傻等下去了。可能是电脑出了别的问题了&#xff0c;来看看教程的说法。正在准备配置windows请勿关闭计算机时间长了方法一…...

    2022/11/19 21:16:59
  43. 配置失败还原请勿关闭计算机,配置Windows Update失败,还原更改请勿关闭计算机...

    我们使用电脑的过程中有时会遇到这种情况&#xff0c;当我们打开电脑之后&#xff0c;发现一直停留在一个界面&#xff1a;“配置Windows Update失败&#xff0c;还原更改请勿关闭计算机”&#xff0c;等了许久还是无法进入系统。如果我们遇到此类问题应该如何解决呢&#xff0…...

    2022/11/19 21:16:58
  44. 如何在iPhone上关闭“请勿打扰”

    Apple’s “Do Not Disturb While Driving” is a potentially lifesaving iPhone feature, but it doesn’t always turn on automatically at the appropriate time. For example, you might be a passenger in a moving car, but your iPhone may think you’re the one dri…...

    2022/11/19 21:16:57