我在 Typescript 中使用 ReactJS。我需要下面的“构造函数”代码吗?没有它它也能正常工作,我查看了转码后的 JavaScript,它似乎无论如何都会自动添加它。
interface myProps {
children?: any;
}
class MyButton extends React.Component<myProps, {}> {
constructor(props: myProps) { //Needed ???
super(props);
}
render() {
return (<div>
<button>
{this.props.children}
</button>
</div>);
} //end render.
} //end class.
最佳答案
不,你不需要。
事实上,您可以将这样一个简单的组件编写为一个函数。
const MyButton = (props) => {
return (
<div><button>{props.children}</button></div>
);
};
关于reactjs - ReactJS 组件类是否需要构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35441713/