I found some tutorial that explains how to build the simplest project: Tutorial: Create a Node.js and React app in Visual Studio
I succeeded to build a project that shows a message "Welcome to React!!". This project contains only one file in TypeScript: app.tsx. Now I want to build a project containing some additional modules written in React and having extension ".js". It seems that these files should be transpiled to TypeScript. But I did not find how to do that in this project. For example in app.tsx there is a code
import Button from "./Button";
...
ReactDOM.render(<Button />, document.getElementById('root'));
and I have file Button.js that contains
...
handleClick = () => {
this.props.clickHandler(this.props.name);
};
When I run in command line
node_modules\.bin\webpack app.tsx --config webpack-config.js
I get an error message:
ERROR in ./Button.js 11:14
Module parse failed: Unexpected token (11:14)
You may need an appropriate loader to handle this file type,
currently no loaders are configured to process this file. See
https://webpack.js.org/concepts#loaders
| //};
|
> handleClick = () => {
| this.props.clickHandler(this.props.name);
| };
In npm folder I have ts-loader, so I do not understand what the problem? Update 1. I did changes as Rich N suggested and now it works. But I still want to use the original content of Button.js:
import React from "react";
import PropTypes from "prop-types";
//import "./Button.css";
export default class Button extends React.Component {
static propTypes = {
name: PropTypes.string,
clickHandler: PropTypes.func,
};
handleClick = () => {
this.props.clickHandler(this.props.name);
};
setImage= () => {
var img="./Images/";
switch(this.props.name)
{
case "move":
img+="Move.jpg";
break;
case "drawPoint":
img+="Point.jpg";
break;
default:
console.log("default image")
}
return img;
};
render() {
return (
<div >
<button onClick={this.handleClick}>
<img src={this.setImage()} alt=""></img>
</button>
</div>
);
}
}
When I try to run the project I receive a number of error messages. For example:
Error TS2339 (TS) Property 'props' does not exist on type 'Button'.
I think this is because not each JavaScript code is valid TypeScript code. So I still have 2 questions:
- How change this code so that it will be right TypeScript code?
- Is there any utility that allows to do that automatically, not manually?