Javascript react-如何从另一个类传递事件 我所拥有的
我有一个类,它没有被导出,而是被一个文件在内部使用。(这些类都在同一个文件中)Javascript react-如何从另一个类传递事件 我所拥有的,javascript,reactjs,dom-events,Javascript,Reactjs,Dom Events,我有一个类,它没有被导出,而是被一个文件在内部使用。(这些类都在同一个文件中) me是事件 我得到了什么 控制台发布以下内容: search results <react></react> undefined 搜索结果 未定义 因此,由于某种原因,event.target是,而me是未定义的 预期行为 它应该返回元素,即您没有传递事件对象 改变这个 <div className="search-results-item" onClick={()
me
是事件
我得到了什么
控制台发布以下内容:
search results <react></react>
undefined
搜索结果
未定义
因此,由于某种原因,event.target是
,而me
是未定义的
预期行为
它应该返回元素,即您没有传递
事件
对象
改变这个
<div
className="search-results-item"
onClick={() => this.fill_name(event, this.props.name)}
/>
this.fill\u name(事件,this.props.name)}
/>
对此
<div
className="search-results-item"
// pass the event here
onClick={event => this.fill_name(event, this.props.name)}
/>
this.fill\u name(事件,this.props.name)}
/>
这应该适合您:
class SearchResults extends Component {
constructor() {
super();
this.fill_name = this.fill_name.bind(this);
}
fill_name() {
this.props.fill_name(this.props.name, this.ref)
}
render() {
return (
<div className="search-results-item" ref={ref => { this.ref = ref }} onClick={this.fill_name}>
<div className="highlight">
{this.props.name}
</div>
</div>
)
}
}
类搜索结果扩展组件{
构造函数(){
超级();
this.fill\u name=this.fill\u name.bind(this);
}
填写姓名(){
this.props.fill_name(this.props.name,this.ref)
}
render(){
返回(
{this.ref=ref}}onClick={this.fill_name}>
{this.props.name}
)
}
}
在6分钟内完成标记。奇怪,因为我有其他的活动材料,我不会这样称呼它们。你能展示一下你是如何使用它们的吗?我们可以讨论他们如何/为什么工作/不工作!啊,它们在工作,但可能是因为它们被隐式传递了。所以我没有向它们传递任何变量。是的,这是更好的方法:onClick={this.fill_name}
,因为当您这样做时:onClick={(e)=>this.fillname(e)}
每次调用render
函数时都会创建该函数,从您的代码判断,您可以避免这种情况!我需要更多地查看您的代码,但感觉您应该能够避免使用jQuery操作#company_input
元素。也不清楚为什么你首先需要通过div。这感觉不对-应该有一种不直接使用DOM的方法来做事情。@Michael它现在实际上相当动态,我需要相对于我所单击的内容来获取它,所以不要使用$(“#公司输入”).val(名称)现在是让parent_val=$(me.parent().prev()$(父/母)val(名称)代码>。
<div
className="search-results-item"
// pass the event here
onClick={event => this.fill_name(event, this.props.name)}
/>
class SearchResults extends Component {
constructor() {
super();
this.fill_name = this.fill_name.bind(this);
}
fill_name() {
this.props.fill_name(this.props.name, this.ref)
}
render() {
return (
<div className="search-results-item" ref={ref => { this.ref = ref }} onClick={this.fill_name}>
<div className="highlight">
{this.props.name}
</div>
</div>
)
}
}