admin管理员组文章数量:1406334
I am trying to Hello World React with Typescript. I wrote the code below. This code works when I use method 1, but throws error on method 2
Method 1
- TypeScriptComponent.tsx
import React from 'react'
import ReactDOM from 'react-dom'
import Button from 'react-bootstrap/lib/Button'
interface TypeScriptComponentProps {
}
function handleClick() {
console.log("Hello World")
}
export const TypeScriptComponent = (props: TypeScriptComponentProps) => <Button onclick={handleClick()} bsClass="glyphicon glyphicon-new-window"></Button>;
This code works (with a bit unexpected behaviour with handleClick function, which I can resolve later.)
But this method wont work
Method 2
- TypeScriptComponent.tsx
import React from 'react'
import ReactDOM from 'react-dom'
import Button from 'react-bootstrap/lib/Button'
interface TypeScriptComponentProps {
}
function handleClick() {
console.log("Hello World")
}
export default class TypeScriptComponent extends React.Component<TypeScriptComponentProps, {}> {
render() {
return (<Button onclick={handleClick()} bsClass="glyphicon glyphicon-new-window"></Button>)
}
}
Method 2 throws error
ERROR in [at-loader] ./ponents/TypeScriptComponent.tsx:43:70 TS2339: Property '_ proto _' does not exist on type '() => any'.
ERROR in [at-loader] ./ponents/TypeScriptComponent.tsx:46:5 TS2346: Supplied parameters do not match any signature of call target.
I am not able to figure out what is the difference between the two methods? Why those errors??
I am trying to Hello World React with Typescript. I wrote the code below. This code works when I use method 1, but throws error on method 2
Method 1
- TypeScriptComponent.tsx
import React from 'react'
import ReactDOM from 'react-dom'
import Button from 'react-bootstrap/lib/Button'
interface TypeScriptComponentProps {
}
function handleClick() {
console.log("Hello World")
}
export const TypeScriptComponent = (props: TypeScriptComponentProps) => <Button onclick={handleClick()} bsClass="glyphicon glyphicon-new-window"></Button>;
This code works (with a bit unexpected behaviour with handleClick function, which I can resolve later.)
But this method wont work
Method 2
- TypeScriptComponent.tsx
import React from 'react'
import ReactDOM from 'react-dom'
import Button from 'react-bootstrap/lib/Button'
interface TypeScriptComponentProps {
}
function handleClick() {
console.log("Hello World")
}
export default class TypeScriptComponent extends React.Component<TypeScriptComponentProps, {}> {
render() {
return (<Button onclick={handleClick()} bsClass="glyphicon glyphicon-new-window"></Button>)
}
}
Method 2 throws error
ERROR in [at-loader] ./ponents/TypeScriptComponent.tsx:43:70 TS2339: Property '_ proto _' does not exist on type '() => any'.
ERROR in [at-loader] ./ponents/TypeScriptComponent.tsx:46:5 TS2346: Supplied parameters do not match any signature of call target.
I am not able to figure out what is the difference between the two methods? Why those errors??
Share Improve this question asked Apr 4, 2017 at 9:03 iamsakshamiamsaksham 2,9494 gold badges30 silver badges52 bronze badges 2-
Can you tell use which lines are
43
and46
please. This would help to identify your problem. – Sebastian Sebald Commented Apr 4, 2017 at 9:18 -
Thats an issue as well @SebastianSebald , I'm using Typescript with webpack, so it messes with line numbers. My file
TypeScriptComponent.tsx
looks exactly same as I posted. There are no 43/46 lines – iamsaksham Commented Apr 4, 2017 at 9:22
1 Answer
Reset to default 4Your code has multiple issues.
- Because TypeScript adheres the ESModule specification you have to import React with
* as React from 'react'
. Same for ReactDOM. - If I am using your input path for the
Button
TypeScript can not find anything there. - If I am using the correct path, TypeScript will tell you that there is no
onclick
prop onButton
. Only anonClick
. - The way you're passing the click handler to
onClick
doesn't make any sense. You have to pass the function not the return value of the function.
Here is what works for me:
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Button } from 'react-bootstrap';
interface TypeScriptComponentProps {
}
function handleClick() {
console.log('Hello World');
}
class App extends React.Component<TypeScriptComponentProps, {}> {
render() {
return (<Button onClick={handleClick} bsClass="glyphicon glyphicon-new-window">Click Me!</Button>)
}
}
ReactDOM.render(
<App />,
document.getElementById('root') as HTMLElement
);
My config is using ts-loader
not at-loader
. Did you add the CheckPlugin
to your webpack config? Otherwhise you will not get any type errors.
If you're new or unfamiliar with Webpack/TypeScript/React-bo, I would suggest checking out this fork of create-react-app
: https://github./wmonk/create-react-app-typescript :)
本文标签: javascriptReactTypescript Hello WorldStack Overflow
版权声明:本文标题:javascript - React-Typescript Hello World - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745005085a2637214.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论