How to use the react-is.isValidElementType function in react-is

To help you get started, weโ€™ve selected a few react-is examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mui-org / material-ui / packages / material-ui-utils / src / componentPropType.js View on Github external
return function componentPropType(props, key, componentName, location, propFullName) {
    const prop = props[key];
    const propName = propFullName || key;
    let message;

    if (prop == null) {
      if (isRequired) {
        message =
          `The ${location} \`${propName}\` is marked as required in \`${componentName}\`, ` +
          `but its value is \`${typeof prop}\`.`;
      }
    } else if (!isValidElementType(prop)) {
      const preciseType = typeof prop;
      message =
        `Invalid ${location} \`${propName}\` of type \`${preciseType}\` ` +
        `supplied to \`${componentName}\`, expected a component.`;
    }

    if (message != null) {
      // change error message slightly on every check to prevent caching when testing
      // which would not trigger console errors on subsequent fails
      return new Error(`${message}${process.env.NODE_ENV === 'test' ? Date.now() : ''}`);
    }

    return null;
  };
}
github styled-components / styled-components / packages / benchmarks / src / implementations / styled-components-v4 / styled-components-v4.esm.js View on Github external
function constructWithOptions(componentConstructor, tag) {
  var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : EMPTY_OBJECT;

  if (!isValidElementType(tag)) {
    throw new StyledComponentsError(1, String(tag));
  }

  /* This is callable directly as a template function */
  // $FlowFixMe: Not typed to avoid destructuring arguments
  var templateFunction = function templateFunction() {
    return componentConstructor(tag, options, css.apply(undefined, arguments));
  };

  /* If config methods are called, wrap up a new template function and merge options */
  templateFunction.withConfig = function(config) {
    return constructWithOptions(componentConstructor, tag, _extends({}, options, config));
  };

  /* Modify/inject new props at runtime */
  templateFunction.attrs = function(attrs) {
github uber / baseweb / src / helpers / overrides.js View on Github external
export function toObjectOverride(
  override: OverrideT,
): OverrideObjectT {
  if (isValidElementType(override)) {
    return {
      // eslint-disable-next-line flowtype/no-weak-types
      component: ((override: any): React.ComponentType),
    };
  }

  // Flow can't figure out that typeof 'function' above will
  // catch React.StatelessFunctionalComponent
  // (probably related to https://github.com/facebook/flow/issues/6666)
  // eslint-disable-next-line flowtype/no-weak-types
  return ((override || {}: any): OverrideObjectT);
}
github zeit / next.js / packages / next / build / utils.ts View on Github external
export async function isPageStatic(
  page: string,
  serverBundle: string,
  runtimeEnvConfig: any
): Promise<{
  static?: boolean
  prerender?: boolean
  isHybridAmp?: boolean
  prerenderRoutes?: string[] | undefined
}> {
  try {
    require('../next-server/lib/runtime-config').setConfig(runtimeEnvConfig)
    const mod = require(serverBundle)
    const Comp = mod.default || mod

    if (!Comp || !isValidElementType(Comp) || typeof Comp === 'string') {
      throw new Error('INVALID_DEFAULT_EXPORT')
    }

    const hasGetInitialProps = !!(Comp as any).getInitialProps
    const hasStaticProps = !!mod.unstable_getStaticProps
    const hasStaticPaths = !!mod.unstable_getStaticPaths
    const hasLegacyStaticParams = !!mod.unstable_getStaticParams

    if (hasLegacyStaticParams) {
      throw new Error(
        `unstable_getStaticParams was replaced with unstable_getStaticPaths. Please update your code.`
      )
    }

    // A page cannot be prerendered _and_ define a data requirement. That's
    // contradictory!
github callstack / linaria / src / babel / visitors / TaggedTemplateExpression.js View on Github external
let selector = `.${className}`;

  if (styled) {
    // If `styled` wraps another component and not a primitive,
    // get its class name to create a more specific selector
    // it'll ensure that styles are overridden properly
    if (options.evaluate && t.isIdentifier(styled.component.node)) {
      let { value } = evaluate(
        styled.component,
        t,
        state.file.opts.filename,
        undefined,
        options
      );

      while (isValidElementType(value) && value.__linaria) {
        selector += `.${value.__linaria.className}`;
        value = value.__linaria.extends;
      }
    }

    const props = [];

    props.push(
      t.objectProperty(t.identifier('name'), t.stringLiteral(displayName))
    );

    props.push(
      t.objectProperty(t.identifier('class'), t.stringLiteral(className))
    );

    // If we found any interpolations, also pass them so they can be applied
github callstack / linaria / src / babel / evaluate / templateProcessor.ts View on Github external
`The CSS cannot contain JavaScript expressions when using the 'css' tag. To evaluate the expressions at build time, pass 'evaluate: true' to the babel plugin.`
            );
          }
        }
      }
    });

    let selector = `.${className}`;

    if (styled) {
      // If `styled` wraps another component and not a primitive,
      // get its class name to create a more specific selector
      // it'll ensure that styles are overridden properly
      if (options.evaluate && types.isIdentifier(styled.component.node)) {
        let value = valueCache.get(styled.component.node.name);
        while (isValidElementType(value) && hasMeta(value)) {
          selector += `.${value.__linaria.className}`;
          value = value.__linaria.extends;
        }
      }

      const props = [];

      props.push(
        types.objectProperty(
          types.identifier('name'),
          types.stringLiteral(displayName!)
        )
      );

      props.push(
        types.objectProperty(
github facebook / prop-types / factoryWithTypeCheckers.js View on Github external
function validate(props, propName, componentName, location, propFullName) {
      var propValue = props[propName];
      if (!ReactIs.isValidElementType(propValue)) {
        var propType = getPropType(propValue);
        return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + propType + '` supplied to `' + componentName + '`, expected a single ReactElement type.'));
      }
      return null;
    }
    return createChainableTypeChecker(validate);
github reactioncommerce / reaction-component-library / package / src / utils / CustomPropTypes.js View on Github external
function checkType(isRequired, props, propName, componentName, location, propFullName) {
    const val = props[propName];
    if (val === null || val === undefined) {
      if (isRequired) {
        if (val === null) {
          return new PropTypeError(`The ${location} '${propFullName}' is marked as required in '${componentName}', but its value is 'null'.`);
        }
        return new PropTypeError(`The ${location} '${propFullName}' is marked as required in '${componentName}', but its value is 'undefined'.`);
      }
    } else if (!isValidElementType(val)) {
      return new PropTypeError(`Invalid ${location} '${propFullName}' supplied to ${componentName}. ` +
          "Expected a string (for built-in components) or a class/function (for composite components).");
    }
    return null;
  }
github airbnb / enzyme / packages / enzyme-adapter-react-16 / src / ReactSixteenAdapter.js View on Github external
isValidElementType(object) {
    return !!object && isValidElementType(object);
  }
github commercetools / ui-kit / src / components / tooltip / tooltip.js View on Github external
BodyComponent: (props, propName) => {
      if (props[propName] && !isValidElementType(props[propName])) {
        return new Error(
          `Invalid prop 'components.BodyComponent' supplied to 'Tooltip': the prop is not a valid React component`
        );
      }
      return null;
    },
    TooltipWrapperComponent: (props, propName) => {