React refs 是如何使用的,什么时候使用的?
React refs how are they used, and when are they used?
您好,感谢您阅读这个问题!
我已经学习 React 几个星期了,我很难理解 refs 如何获取 React 的实例并将其放入 JS 变量中。
例如,我们可以讨论文档的示例:
class CustomTextInput extends React.Component {
constructor(props) {
super(props);
this.focusTextInput = this.focusTextInput.bind(this);
}
focusTextInput() {
// Explicitly focus the text input using the raw DOM API
this.textInput.focus();
}
render() {
// Use the `ref` callback to store a reference to the text input DOM
// element in an instance field (for example, this.textInput).
return (
<div>
<input
type="text"
ref={(input) => { this.textInput = input; }} />
<input
type="button"
value="Focus the text input"
onClick={this.focusTextInput}
/>
</div>
);
}
}
我知道 ref 获取将要呈现的输入元素并使用 this.textInput 将其存储到 class 字段中。
但是我不明白为什么传递给 refs 的参数是(输入)如果嵌套了 jsx 标签会发生什么?例如两个输入?
此外,是否没有明确的方法来引用使用 React 创建的元素 render/return?我说的是面向对象编程之类的东西:className.instanceName 或从 HTML 元素创建实例:new elementName().
感谢您的帮助!
React 支持可以附加到任何组件的特殊属性。 ref
属性带回调函数,callback
会在组件挂载或卸载后立即执行。
写的时候
<input
type="text"
ref={(input) => { this.textInput = input; }} />
React 提取 ref
属性并在组件安装后调用回调。在该回调函数中,React 传递了输入的实例,这就是您在此处作为参数获得的内容。将以上视为一个函数
<input
type="text"
ref={this.callback} />
回调看起来像
const callback = (input) => {
this.textInput = input;
}
根据文档:
When the ref attribute is used on an HTML element, the ref
callback
receives the underlying DOM element as its argument.
关于你的下一个问题:
However I do not understand why the parameter being passed to the refs
is (input) what could happen if there were jsx tags nested
传递给 div 的参数只是在您的示例中作为输入引用,您可以随意调用它,例如 input
、inputRef
、instance
嵌套多个jsx标签时,ref应用于传递ref属性的元素。例如
<div ref={this.getRef}>
<div>Hello React</div>
</div>
ref
获取的实例应用于外部 div,您可以从中访问 child 元素。
Also is there not a clear way to reference the elements being created
with React render/return
嗯refs
是React提供的一种引用、创建元素的方式。但是你应该只在
时使用 refs
- 管理焦点、文本选择或媒体播放。
- 触发命令式动画。
- 与 third-party DOM 库集成。
大多数情况下,props
是 parent 组件与其 children 交互的唯一方式。要修改 child,您需要 re-render 使用新道具。例如,如果您希望更改元素上的 class,而不是访问元素并更改它 class,您可以将动态属性传递给它,而不是像
<div className={this.props.className} />
或者事实上,使用 state
进行必要的更新
为了解释 ref
标签发生了什么,让我们把它分解成更小的部分...
这个区块:
<input
type="text"
ref={(input) => { this.textInput = input; }} />
表示在安装和卸载此 input
字段时调用此 (input) => { this.textInput = input; }
。 (input
) 只是一个参数名称,您可以随意调用它。所以你可以将其重写为:
<input
type="text"
ref={(myinputfield) => { this.textInput = myinputfield; }} />
它会做同样的事情。在这两种情况下,input
和 myinputfield
都引用定义了 ref
属性的文本字段。
关于你的第二个问题,
Also is there not a clear way to reference the elements being created
with React render/return? I am talking about something like object
oriented programming: className.instanceName or creating instance from
the HTML elements with: new elementName().
反应模型有点不同。 state
是让组件相互交互的主要方式,而不是一个组件调用另一个组件。尚不完全清楚您要做什么,但是当您想根据另一个组件执行的某些操作更新一个组件时,您通常会更新 state
,这将 re-render 组件与新状态。
您仍然可以在 DOM 中查找其他组件,但我鼓励您多考虑使用状态更新组件的 React 模型。
您好,感谢您阅读这个问题!
我已经学习 React 几个星期了,我很难理解 refs 如何获取 React 的实例并将其放入 JS 变量中。
例如,我们可以讨论文档的示例:
class CustomTextInput extends React.Component {
constructor(props) {
super(props);
this.focusTextInput = this.focusTextInput.bind(this);
}
focusTextInput() {
// Explicitly focus the text input using the raw DOM API
this.textInput.focus();
}
render() {
// Use the `ref` callback to store a reference to the text input DOM
// element in an instance field (for example, this.textInput).
return (
<div>
<input
type="text"
ref={(input) => { this.textInput = input; }} />
<input
type="button"
value="Focus the text input"
onClick={this.focusTextInput}
/>
</div>
);
}
}
我知道 ref 获取将要呈现的输入元素并使用 this.textInput 将其存储到 class 字段中。
但是我不明白为什么传递给 refs 的参数是(输入)如果嵌套了 jsx 标签会发生什么?例如两个输入?
此外,是否没有明确的方法来引用使用 React 创建的元素 render/return?我说的是面向对象编程之类的东西:className.instanceName 或从 HTML 元素创建实例:new elementName().
感谢您的帮助!
React 支持可以附加到任何组件的特殊属性。 ref
属性带回调函数,callback
会在组件挂载或卸载后立即执行。
写的时候
<input
type="text"
ref={(input) => { this.textInput = input; }} />
React 提取 ref
属性并在组件安装后调用回调。在该回调函数中,React 传递了输入的实例,这就是您在此处作为参数获得的内容。将以上视为一个函数
<input
type="text"
ref={this.callback} />
回调看起来像
const callback = (input) => {
this.textInput = input;
}
根据文档:
When the ref attribute is used on an HTML element, the
ref
callback receives the underlying DOM element as its argument.
关于你的下一个问题:
However I do not understand why the parameter being passed to the refs is (input) what could happen if there were jsx tags nested
传递给 div 的参数只是在您的示例中作为输入引用,您可以随意调用它,例如 input
、inputRef
、instance
嵌套多个jsx标签时,ref应用于传递ref属性的元素。例如
<div ref={this.getRef}>
<div>Hello React</div>
</div>
ref
获取的实例应用于外部 div,您可以从中访问 child 元素。
Also is there not a clear way to reference the elements being created with React render/return
嗯refs
是React提供的一种引用、创建元素的方式。但是你应该只在
- 管理焦点、文本选择或媒体播放。
- 触发命令式动画。
- 与 third-party DOM 库集成。
大多数情况下,props
是 parent 组件与其 children 交互的唯一方式。要修改 child,您需要 re-render 使用新道具。例如,如果您希望更改元素上的 class,而不是访问元素并更改它 class,您可以将动态属性传递给它,而不是像
<div className={this.props.className} />
或者事实上,使用 state
进行必要的更新
为了解释 ref
标签发生了什么,让我们把它分解成更小的部分...
这个区块:
<input
type="text"
ref={(input) => { this.textInput = input; }} />
表示在安装和卸载此 input
字段时调用此 (input) => { this.textInput = input; }
。 (input
) 只是一个参数名称,您可以随意调用它。所以你可以将其重写为:
<input
type="text"
ref={(myinputfield) => { this.textInput = myinputfield; }} />
它会做同样的事情。在这两种情况下,input
和 myinputfield
都引用定义了 ref
属性的文本字段。
关于你的第二个问题,
Also is there not a clear way to reference the elements being created with React render/return? I am talking about something like object oriented programming: className.instanceName or creating instance from the HTML elements with: new elementName().
反应模型有点不同。 state
是让组件相互交互的主要方式,而不是一个组件调用另一个组件。尚不完全清楚您要做什么,但是当您想根据另一个组件执行的某些操作更新一个组件时,您通常会更新 state
,这将 re-render 组件与新状态。
您仍然可以在 DOM 中查找其他组件,但我鼓励您多考虑使用状态更新组件的 React 模型。