我正在使用带有React的webpack,我不能在生活中了解这个版本中发生了什么 . 这应该是正在发生的事情 .
-
var headerInput 更改为输入的任何值 onChange .
-
提交表单时( onSubmit ),console.log记录 headerInput 值 .
The problem :获取控制台记录的值是数字,它是否像在handlerInput函数中一样分配了值?'s usually something like: .0.0.1. I think it' s console.log 'ing the click event. Why isn' t?
任何帮助深表感谢 . 谢谢,全部 .
var headerInput = null;
import React from "react";
export default class Navigation extends React.Component{
handlerInput(e,headerInput){
headerInput = e.target.value;
console.log(headerInput);
};
clickSubmit(e,headerInput){
e.preventDefault();
console.log(headerInput);
};
render(){
return(
<form onSubmit={this.clickSubmit.bind(this)}>
<input type="text" placeholder="change header" onChange={this.handlerInput.bind(this)} />
<button>Change Header</button>
</form>
);
}
};
2 回答
这不是使用React的推荐方法 . 您应该使用组件附带的状态API,而不是依赖“全局”来存储您的状态 .
像这样:
我绝对建议你重温官方的反应文档,比如thinking in react和react forms教程 .
如果输入是严格单向的(你只读取它),那么只需使用ref
注意...
https://facebook.github.io/react/docs/more-about-refs.html