reactjs.org官方网站包含了一个优秀的入门教程.
教程片段是用JavaScript编写的,我正在try 将它们转换为TypeScript.
我已经设法让代码正常工作,但对使用接口有疑问.
onClick回调的正确"函数签名"应该是什么.
Is there a way to replace the 'any' keyword in the IProps_Square interface with an explicit function signature ?
任何帮助或建议都将不胜感激,非常感谢Russell
index.html
<!DOCTYPE html>
<html lang="en">
<body>
<div id="reactjs-tutorial"></div>
</body>
</html>
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
interface IProps_Square {
message: string,
onClick: any,
}
class Square extends React.Component < IProps_Square > {
render() {
return (
<button onClick={this.props.onClick}>
{this.props.message}
</button>
);
}
}
class Game extends React.Component {
render() {
return (
<Square
message = { 'click this' }
onClick = { () => alert('hello') }
/>
);
}
}
ReactDOM.render(
<Game />,
document.getElementById('reactjs-tutorial')
);