【发布时间】:2020-07-17 15:49:01
【问题描述】:
我正在创建一个 REACT 组件,它通过基本上接受用户的输入并使用简单的键值对访问翻译来“翻译”一个数字。除了我的 handleTranslate 方法外,一切正常。此方法的控制台日志未定义。
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
input: '',
one: 'uno',
two: 'dos',
three: 'tres',
four: 'cuatro',
five: 'cinco',
six: 'seis',
seven: 'siete',
eight: 'ocho',
nine: 'nueve',
ten: 'diez',
answer: ''
};
this.handleChange = this.handleChange.bind(this);
this.handleTranslate = this.handleTranslate.bind(this);
};
handleChange(state) {
this.setState({
input: event.target.value
});
}
handleTranslate (state) {
var x = state.input;
this.setState({
answer: state.x
});
}
render () {
return(
<div>
<h3>Enter an English number between one and ten and watch the translation render below</h3>
<input value={this.state.value} onChange={this.handleChange, this.handleTranslate}/>
<p>{this.state.input}</p>
</div>
);
}
};
ReactDOM.render(<MyComponent/>, document.getElementById('view'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id='view' />
【问题讨论】:
-
顺便说一句,一旦方法被修复或与 handleChange 结合,我将不得不更改我的
标签内的内容。
-
这里是我的一支笔的链接,它以类似的方式运作,确实有效:codepen.io/adamjschork/pen/QWbPeOV
标签: javascript reactjs react-component react-state-management react-state