當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript react-is.isElement函數代碼示例

本文整理匯總了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];
}
開發者ID:skidding,項目名稱:cosmos,代碼行數:28,代碼來源:findElementPaths.ts

示例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>;
}
開發者ID:skidding,項目名稱:cosmos,代碼行數:17,代碼來源:getElementAtPath.ts

示例3: isMultiFixture

export function isMultiFixture(
  fixtureExport: ReactFixtureExport
): fixtureExport is ReactFixtureMap {
  return (
    fixtureExport !== null &&
    typeof fixtureExport === 'object' &&
    !isElement(fixtureExport)
  );
}
開發者ID:skidding,項目名稱:cosmos,代碼行數:9,代碼來源:isMultiFixture.ts

示例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)
    )
  };
}
開發者ID:skidding,項目名稱:cosmos,代碼行數:19,代碼來源:areNodesEqual.ts

示例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;
}
開發者ID:skidding,項目名稱:cosmos,代碼行數:20,代碼來源:setElementAtPath.ts

示例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)
  };
}
開發者ID:skidding,項目名稱:cosmos,代碼行數:23,代碼來源:shared.ts

示例7:

export const test = (val: any) => val && ReactIs.isElement(val);
開發者ID:Volune,項目名稱:jest,代碼行數:1,代碼來源:ReactElement.ts


注:本文中的react-is.isElement函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。