本文整理汇总了TypeScript中react-is.isElement函数的典型用法代码示例。如果您正苦于以下问题:TypeScript isElement函数的具体用法?TypeScript isElement怎么用?TypeScript isElement使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isElement函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: findElementPaths
export function findElementPaths(
node: React.ReactNode,
curPath: string = ''
): string[] {
if (Array.isArray(node)) {
return flatten(
node.map((child, idx) => findElementPaths(child, `${curPath}[${idx}]`))
);
}
if (!isElement(node)) {
// At this point the node can be null, boolean, string, number, Portal, etc.
// https://github.com/facebook/flow/blob/172d28f542f49bbc1e765131c9dfb9e31780f3a2/lib/react.js#L13-L20
return [];
}
const element = node as React.ReactElement<any>;
const { children } = element.props;
const childElPaths =
// Props of elements returned by render functions can't be read here
typeof children !== 'function'
? findElementPaths(children, getChildrenPath(curPath))
: [];
// Ignore Fragment elements, but include their children
return element.type === Fragment ? childElPaths : [curPath, ...childElPaths];
}
示例2: getElementAtPath
export function getElementAtPath(
node: React.ReactNode,
elPath: string
): null | React.ReactElement<any> {
if (!isElement(node) && !Array.isArray(node)) {
return null;
}
const rootNode = node as React.ReactElement<any> | React.ReactNode[];
const childNode = isRootPath(elPath) ? rootNode : get(rootNode, elPath);
if (!isElement(childNode)) {
return null;
}
return childNode as React.ReactElement<any>;
}
示例3: isMultiFixture
export function isMultiFixture(
fixtureExport: ReactFixtureExport
): fixtureExport is ReactFixtureMap {
return (
fixtureExport !== null &&
typeof fixtureExport === 'object' &&
!isElement(fixtureExport)
);
}
示例4: stripInternalElementAttrs
// Don't compare private element attrs like _owner and _store, which hold
// internal details and have auto increment-type attrs
function stripInternalElementAttrs(node: React.ReactNode): {} {
if (Array.isArray(node)) {
return node.map(n => stripInternalElementAttrs(n));
}
if (!isElement(node)) {
return node;
}
const el = node as React.ReactElement<any>;
return {
...pick(el, 'type', 'key', 'ref'),
// children and other props can contain Elements
props: mapValues(el.props, propValue =>
stripInternalElementAttrs(propValue)
)
};
}
示例5: cloneNode
function cloneNode(value: React.ReactNode): React.ReactNode {
if (Array.isArray(value)) {
return value.map(n => cloneNode(n));
}
if (isElement(value)) {
const el = value as React.ReactElement<any>;
const { children, ...otherProps } = el.props;
return {
...el,
props: {
...otherProps,
children: cloneNode(children)
}
};
}
return value;
}
示例6: stringifyValue
function stringifyValue(key: string, value: unknown): FixtureStateValue {
try {
// NOTE: Is this optimal?
if (!isEqual(JSON.parse(JSON.stringify(value)), value)) {
throw new Error('Unserializable value');
}
} catch (err) {
return {
serializable: false,
key,
// TODO: Enable custom stringifiers to plug in
stringified: isElement(value)
? reactElementToJSXString(value)
: String(value)
};
}
return {
serializable: true,
key,
stringified: JSON.stringify(value)
};
}
示例7:
export const test = (val: any) => val && ReactIs.isElement(val);