Giter Site home page Giter Site logo

Comments (5)

svdoever avatar svdoever commented on May 7, 2024 2

@ufukomer the typings were already installed. The error was because I had to do const MyComponent = require("./MyComponent").default;. I decided to write down some info about using Javascriptcomponents from TypeScript.

Consuming Javascript ReactJS components from TypeScript

There are cases where you need to consume ReactJS components written in Javascript from your TypeScript code. Possible reasons:

  • You find a great component "out in the wild" written in Javascript
  • One of your team-members does not know/does not want to write TypeScript
  • You want to consume components from an existing component set writen in Javascript

Assume the situation where a component MyComponent.jsx written in Javascript must be consumed from a component Home.tsx:

The file MyComponent.jsx:

import * as React from 'react';

export class MyComponent extends React.Component {
    constructor(props) { super(props); }
    render() { return (<h1>Hello from MyComponent</h1>); }
}

We could import this component in different ways:

import {MyComponent} from './MyComponent'; // ERROR: Cannot find module './MyComponent'
const MyComponent = require('./MyComponent'); // Warning if used as <MyComponent/> - see https://codereviewvideos.com/blog/warning-react-createelement/
const X = require('./MyComponent'); // OK if used as <X.MyComponent/>
const MyComponent = require('./MyComponent').MyComponent; // OK
const MyComponent = require('./MyComponent').default; // OK if defined as export default class MyComponent

The file Home.tsx:

import * as React from 'react';
const MyComponent = require('./MyComponent').MyComponent;

export class Home extends React.Component<any, any> {
  render() { return (<div><MyComponent /></div>); }
}

from vortigern.

svdoever avatar svdoever commented on May 7, 2024

I solved the error above by creating a file src/app/components/MyComponent/MyComponent.d.ts containing:

import React = require("react");

export interface MyComponentProps extends React.Props<MyComponentClass> {
}

export interface MyComponent extends React.ReactElement<MyComponentProps> { }
export interface MyComponentClass extends React.ComponentClass<MyComponentProps> { }

export var MyComponent: MyComponentClass;

I also aded react support for jsx files as follows:

  • Add babel react preset package from npm:
npm install babel-preset-react --save-dev
  • Add a .babelrc file to the root with the following contents:
{
  "presets": [
    "es2015",
    "react"
  ]
}

from vortigern.

batuhan avatar batuhan commented on May 7, 2024

Hey @svdoever,

When you are mixing JavaScript libraries with TypeScript ones, you can use good ol' require instead of using definitions (but definitions are always recommended).

For local files:

const MyComponent = require('./MyComponent/MyComponent');

For external modules:

const perfectLib = require('perfectLib');

Apart from external libraries, I'm not sure why would want to mix regular JavaScript with TypeScript in a project :)

from vortigern.

svdoever avatar svdoever commented on May 7, 2024

Would be great if that approach works, without the necessity of type definition files, although those would help. I added MyComponent.jsx file to the Home folder:

import * as React from 'react';

class MyComponent extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <h1>Hello from MyComponent</h1>
        );
    }
}

export default MyComponent;

and changed Home.tsx to:

import * as React from 'react';
const MyComponent = require('./MyComponent');
const s = require('./style.css');

class Home extends React.Component<any, any> {
  render() {
    return (
      <div className={s.home}>
        <img src={require('./barbar.png')} />
        <p>Hello world!</p>
        <MyComponent />
      </div>
    );
  }
}

export { Home }

but keep getting the error:

Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components). Check the render method of `Home`.

Any idea what I'm doing wrong?

from vortigern.

ufukomer avatar ufukomer commented on May 7, 2024

@svdoever try to install typings:

$ typings install dt~react --global --save

from vortigern.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.