对react(?)或javascript语法的好奇
在浏览react documentation()时,我发现了以下代码片段:对react(?)或javascript语法的好奇,javascript,jquery,reactjs,syntax,jquery-chosen,Javascript,Jquery,Reactjs,Syntax,Jquery Chosen,在浏览react documentation()时,我发现了以下代码片段: class Chosen extends React.Component { componentDidMount() { this.$el = $(this.el); this.$el.chosen(); } componentWillUnmount() { this.$el.chosen('destroy'); } render() {
class Chosen extends React.Component {
componentDidMount() {
this.$el = $(this.el);
this.$el.chosen();
}
componentWillUnmount() {
this.$el.chosen('destroy');
}
render() {
return (
<div>
<select className="Chosen-select" ref={el => this.el = el}>
{this.props.children}
</select>
</div>
);
}
}
这句话指的是什么?我知道它与:
ref = { el => {
return this.el = el
}
}
但这意味着什么?这个代码中的流程是什么
el=>this.el=el
是函数(el){this.el=el}
()的排序
ref
关键字用于获取元素引用,以供将来使用,如焦点
事件。我们有两种方法使用ref
方式1(使用ref字符串)
通过这种方式,我们可以在任何我们想要的地方保留ref,因为我们可以通过函数控制它。
el=>this.el=el
是函数(el){this.el=el}()的排序
ref
关键字用于获取元素引用,以供将来使用,如焦点
事件。我们有两种方法使用ref
方式1(使用ref字符串)
通过这种方式,我们可以将ref保留在任何我们想要的地方,因为我们可以通过函数控制它。将
this.el
分配给el
,这只是一个正常的函数,不需要返回该语句。而this.el
用于与DOM节点交互。阅读更多信息将this.el
赋值给el
只是一个普通函数,无需返回该语句。而this.el
用于与DOM节点交互。阅读更多精彩的例子,谢谢,但是分配语法是什么,this.$el=$(this.el)?我从未见过像$el这样的财产名称。这里面有什么意思吗?还是仅仅是一个约定?谢谢您提供了非常好的示例,但是分配语法是什么,this.$el=$(this.el)?我从未见过像$el这样的财产名称。这里面有什么意思吗?还是仅仅是一个会议?
ref = { el => {
return this.el = el
}
}
class MyComponent extends Component {
focusInput() {
this.refs.inputAge.focus();
}
render() {
return(
<input ref="inputAge" />
)
}
}
class MyComponent extends Component {
focusInput() {
this.inputAge.focus();
}
render() {
return(
<input ref={ref => this.inputAge = ref} />
)
}
}