Comments (5)
@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.
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.
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.
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.
@svdoever try to install typings:
$ typings install dt~react --global --save
from vortigern.
Related Issues (20)
- CSS sourcemap is missing in dev.config? HOT 2
- Is it good idea to use express server for developping API code ? HOT 5
- Dockerfile HOT 2
- What are the advantages in bundling server side code? HOT 2
- Unable to have redux-form working
- Upgrade to [email protected]
- Stale DOM classnames: changing component classname requires server restart HOT 1
- the problem of a vendor/main.ts loaded after DOM HOT 2
- Test output not showing correct line numbers
- Order of vendor imports HOT 1
- Barbar Startup Factory link is wrong
- Dupe test declarations for .css files in webpack config?
- webpack v4
- Urgently help
- ERROR in [at-loader] ./node_modules/redux-thunk/index.d.ts:14:84 TS2315: Type 'Middleware' is not generic. HOT 1
- is this project still active? HOT 6
- an error occured when run npm start as the img below
- [request] Add scss support
- Can't Generate PWA, can't cache the server generated strings.
- [solved / for-the-record] Typescript errors on `npm run start`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vortigern.