老师这样可以吗
来源:2-7 自由编程
unbreakable_全栈
2021-01-26 14:39:45
import React, { Component, Fragment } from "react";
class Ref extends Component {
constructor(props) {
super(props);
this.handleInputChange = this.handleInputChange.bind(this);
this.handleBtnClick = this.handleBtnClick.bind(this);
this.state = {
inputValue: "",
};
}
handleInputChange(e) {
// console.log(e.target.value);
this.setState({
inputValue: e.target.value,
});
}
handleBtnClick() {
console.log(this.state.inputValue);
this.setState({
inputValue: "",
});
}
render() {
return (
<Fragment>
{/* ref写在html标签上,获取的是dom节点 */}
<input
ref={(input) => {
this.inputElem = input;
}}
value={this.state.inputValue}
onChange={this.handleInputChange}
/>
<button onClick={this.handleBtnClick}>提交</button>
</Fragment>
);
}
}
export default Ref;
1回答
同学你好,测试同学提供的代码,也是可以实现效果的,但是与编程题要求有些不符,如果按照编程题的要求,那么修改如下:
祝学习愉快~
相似问题