我正在用React创建一个简单的计算器,并且试图向按钮添加onClick函数。我使用tictactoe游戏的react教程中使用了相同的技术,但是当我单击任何按钮时,我只会收到以下错误消息:
Uncaught TypeError: this.handleClick is not a function
at onClick (index.js:71)
at HTMLUnknownElement.callCallback (react-dom.development.js:188)
at Object.invokeGuardedCallbackDev (react-dom.development.js:237)
at invokeGuardedCallback (react-dom.development.js:292)
at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:306)
at executeDispatch (react-dom.development.js:389)
at executeDispatchesInOrder (react-dom.development.js:414)
at executeDispatchesAndRelease (react-dom.development.js:3278)
at executeDispatchesAndReleaseTopLevel (react-dom.development.js:3287)
at forEachAccumulated (react-dom.development.js:3259)
at runEventsInBatch (react-dom.development.js:3304)
at runExtractedPluginEventsInBatch (react-dom.development.js:3514)
at handleTopLevel (react-dom.development.js:3558)
at batchedEventUpdates$1 (react-dom.development.js:21871)
at batchedEventUpdates (react-dom.development.js:795)
at dispatchEventForLegacyPluginEventSystem (react-dom.development.js:3568)
at attemptToDispatchEvent (react-dom.development.js:4267)
at dispatchEvent (react-dom.development.js:4189)
at unstable_runWithPriority (scheduler.development.js:653)
at runWithPriority$1 (react-dom.development.js:11039)
at discreteUpdates$1 (react-dom.development.js:21887)
at discreteUpdates (react-dom.development.js:806)
at dispatchDiscreteEvent (react-dom.development.js:4168)
我的代码如下:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
class Screen extends React.Component {
render() {
return (
<p>99+10</p>
);
}
}
class Operators extends React.Component {
render() {
return (
<div>
{this.props.renderButton('+')}
{this.props.renderButton('-')}
{this.props.renderButton('x')}
{this.props.renderButton('/')}
</div>
);
}
}
class Numbers extends React.Component {
render() {
return (
<div>
<div className="nums-row">
{this.props.renderButton(1)}
{this.props.renderButton(2)}
{this.props.renderButton(3)}
</div>
<div className="nums-row">
{this.props.renderButton(4)}
{this.props.renderButton(5)}
{this.props.renderButton(6)}
</div>
<div className="nums-row">
{this.props.renderButton(7)}
{this.props.renderButton(8)}
{this.props.renderButton(9)}
</div>
<div className="nums-row">
{this.props.renderButton('.')}
{this.props.renderButton(0)}
{this.props.renderButton('=')}
</div>
</div>
);
}
}
function CalculatorButton(props) {
return (
<button onClick={props.onClick} >
{props.text}
</button>
);
}
class Calculator extends React.Component {
handleClick(text) {
console.log(text);
}
renderButton(text) {
return (
<CalculatorButton
onClick= {() => this.handleClick(text)}
text={text}
/>
);
}
render() {
return (
<div className="calculator-container">
<div className="calculator">
<div className="screen">
<Screen />
</div>
<div className="buttons">
<div className="numbers">
<Numbers renderButton={this.renderButton} />
</div>
<div className="operators">
<Operators renderButton={this.renderButton} />
</div>
</div>
</div>
</div>
);
}
}
ReactDOM.render(
<Calculator />,
document.getElementById('root')
);
这是界面的图片,因此可能更容易理解我在问题开始时所说的话。
预先感谢您的帮助。
答案 0 :(得分:3)
您需要将renderButton
函数绑定到Calculator
组件实例。
这可以通过几种方式完成:
#1。在构造函数中:
class Calculator extends React.Component {
constructor(props) {
super(props);
this.renderButton = this.renderButton.bind(this);
}
/* ... */
}
渲染中的#2。:
<Numbers renderButton={this.renderButton.bind(this)} />
#3。或通过将renderButton
方法更改为箭头功能:
renderButton = (text) => {
/* ... */
}
更多详细信息以及一些优缺点可在React faq section on functions中找到。
答案 1 :(得分:1)
this.props.renderButton('+')
调用renderButton
,立即 ,并将返回值分配为事件处理程序。
这是specifically covered in the React documentation:
<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button> <button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>
以上两行是等效的,并使用箭头功能和 分别是Function.prototype.bind。
在两种情况下,代表React事件的e参数将是 作为ID后的第二个参数传递。通过箭头功能,我们 必须显式地传递它,但是使用bind绑定任何其他参数 自动转发。
答案 2 :(得分:1)
要解决您的问题,只需使用箭头功能。为此,请使用以下代码替换两个事件处理程序 handleClick和renderButton 。效果很好。
handleClick = (text) => {
console.log(text);
};
renderButton = (text) => {
return (
<CalculatorButton
onClick={() => this.handleClick(text)}
text={text}
/>
);
};