Javascript React中的event.preventDefault类型错误:事件未定义
我有一个名为Javascript React中的event.preventDefault类型错误:事件未定义,javascript,reactjs,forms,Javascript,Reactjs,Forms,我有一个名为App的React组件,它应该显示基于HTML表单的内容。每次用户提交表单时,App的状态都应该适应。起初,我是这样实现我的类的: class App extends React.Component { constructor(props) { super(props); this.state = {technologies: [], subject: ''}; this.updateSubject = this.updateSubject.bind(th
App
的React组件,它应该显示基于HTML表单的内容。每次用户提交表单时,App
的状态都应该适应。起初,我是这样实现我的类的:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {technologies: [], subject: ''};
this.updateSubject = this.updateSubject.bind(this);
this.updateTimeline = this.updateTimeline.bind(this);
}
componentDidMount() {
this.updateTimeline();
}
updateSubject(event) {
this.setState({subject: event.target.value});
}
updateTimeline() {
fetch('/timeline?subject=' + this.state.subject)
.then(res => res.json())
.then(technologies => this.setState({technologies: technologies}));
}
render() {
return <div>
<form id="subject" onSubmit={this.updateTimeline}>
<label>
Subject:
<input type="text" value={this.state.subject} onChange={this.updateSubject} />
</label>
<input type="submit" value="Submit" />
</form>
<Timeline techs={this.state.technologies} />
</div>;
}
}
这给了我
TypeError:事件在控制台中未定义。为什么会出现这种情况?onSubmit
非常有棱角,而不是这样做,请按如下方式更改代码:
类应用程序扩展了React.Component{
建造师(道具){
超级(道具);
this.state={technologies:[],主题:'};
this.updateSubject=this.updateSubject.bind(this);
this.updateTimeline=this.updateTimeline.bind(this);
}
componentDidMount(){
this.updateTimeline();
}
updateSubject(事件){
this.setState({subject:event.target.value});
}
updateTimeline(){
返回fetch('/timeline?subject='+this.state.subject)
.then(res=>res.json())
.then(technologies=>this.setState({technologies:technologies}));
}
render(){
返回
主题:
提交
;
}
}
除了我接受@Ernesto的答案外,我还找到了一种更一般(更语义?)的方法来获得我想要的行为。这只涉及将updateTimeline
方法从我原来问题中的代码更改为:
updateTimeline(event) {
if (arguments.length > 0) event.preventDefault();
fetch('/timeline?subject=' + this.state.subject)
.then(res => res.json())
.then(technologies => this.setState({technologies: technologies}));
}
。。。这允许保留
标记,这对我来说比按钮标记更具语义。同时,这也很好地处理了用户点击return而不是点击Submit的事件??updateTimeline()
的定义中没有event
参数,这是因为在componentDidMount
中调用它时没有传递任何内容。当然,正如我在问题中指定的,我试图在updateTimeline
的签名中添加一个event
参数。我的问题是,即使在添加了event
参数之后,我仍然得到TypeError:event是未定义的。我可以通过编辑来澄清我的问题。你的解决方案确实很有效。非常感谢。但是您是否也知道为什么event.preventDefault()
在我的原始解决方案中给出了一个错误?onSubmit事件没有获取参数,按您所拥有的方式执行,而只是控制台。记录该事件,您将看到一个未定义的事件,但React文档在此链接的NameForm
示例组件的handleSubmit
方法中使用了一个事件参数:是的,我知道,但是,如果您运行自己的codepen示例,您将看到相同的错误:TypeError:n在成功时每个页面-1833776b2c0be7ae3094.js:1:327655都为空https://static.codepen.io/assets/packs/js/everypage-1833776b2c0be7ae3094.js:1 Nhttps://static.codepen.io/assets/packs/js/everypage-1833776b2c0be7ae3094.js:1 错误包装器https://static.codepen.io/assets/packs/js/vendor-0eeab0dfbd1d48f30824.chunk.js:26 一个错误https://static.codepen.io/assets/packs/js/vendor-0eeab0dfbd1d48f30824.chunk.js:26
updateTimeline(event) {
if (arguments.length > 0) event.preventDefault();
fetch('/timeline?subject=' + this.state.subject)
.then(res => res.json())
.then(technologies => this.setState({technologies: technologies}));
}