<Parent>
<Child1 />
<Child2 />
<Child3 />
</Parent>
我想从 Child2
访问具有 refs="child2refs"
的元素,我该如何做?
<Parent>
<Child1 />
<Child2 />
<Child3 />
</Parent>
我想从 Child2
访问具有 refs="child2refs"
的元素,我该如何做?
如果无法避免使用,请参考从React文档提取的建议模式:
import React, { Component } from 'react';
const Child = ({ setRef }) => <input type="text" ref={setRef} />;
class Parent extends Component {
constructor(props) {
super(props);
this.setRef = this.setRef.bind(this);
}
componentDidMount() {
// Calling a function on the Child DOM element
this.childRef.focus();
}
setRef(input) {
this.childRef = input;
}
render() {
return <Child setRef={this.setRef} />
}
}
父组件将一个绑定到父组件的this
的函数作为属性传递。当React调用子组件的ref
属性setRef
时,它将把子组件的ref
赋值给父组件的childRef
属性。
引用转发是一项可选功能,可以让某些组件接收并向下传递(换句话说,“转发”)到子组件中的引用。
我们可以通过使用 React.forwardRef
创建组件来实现引用的转发。返回的组件的ref属性必须与React.createRef
返回类型相同。每当React挂载DOM节点时,使用React.createRef
创建的ref
的current
属性将指向底层的DOM节点。
import React from "react";
const LibraryButton = React.forwardRef((props, ref) => (
<button ref={ref} {...props}>
FancyButton
</button>
));
class AutoFocus extends React.Component {
constructor(props) {
super(props);
this.childRef = React.createRef();
this.onClick = this.onClick.bind(this);
}
componentDidMount() {
this.childRef.current.focus();
}
onClick() {
console.log("fancy!");
}
render() {
return <LibraryButton onClick={this.onClick} ref={this.childRef} />;
}
}
创建的组件将它们的ref
转发给一个子节点。
function logProps(Component) {
class LogProps extends React.Component {
componentDidUpdate(prevProps) {
console.log('old props:', prevProps);
console.log('new props:', this.props);
}
render() {
const {forwardedRef, ...rest} = this.props;
// Assign the custom prop "forwardedRef" as a ref
return <Component ref={forwardedRef} {...rest} />;
}
}
// Note the second param "ref" provided by React.forwardRef.
// We can pass it along to LogProps as a regular prop, e.g. "forwardedRef"
// And it can then be attached to the Component.
return React.forwardRef((props, ref) => {
return <LogProps {...props} forwardedRef={ref} />;
});
}
请查看React文档中的转发Refs。
LibraryButton
不是一个 class
。你能展示一个在 class LibraryButton extends React.Component
中的例子吗?谢谢。 - new2cppconnect
方法上的选项参数{ withRef: true }
并在HOC中访问包装的组件实例,例如:this.wrappedComponentRef.current.getWrappedInstance()
会很有帮助。请参见此处的在连接组件上使用引用部分:https://itnext.io/advanced-react-redux-techniques-how-to-use-refs-on-connected-components-e27b55c06e34#63a4。 - rdrw/*
* Child component
*/
class Child extends React.Component {
render() {
return (
<div id="child">
<h1 ref={(node) => { this.heading = node; }}>
Child
</h1>
</div>
);
}
}
/*
* Parent component
*/
class Parent extends React.Component {
componentDidMount() {
// Access child component refs via parent component instance like this
console.log(this.child.heading.getDOMNode());
}
render() {
return (
<div>
<Child
ref={(node) => { this.child = node; }}
/>
</div>
);
}
}
getDOMNode()
的情况下只使用 this.child.heading
来完成此操作。 - Ian J Millerthis.child.heading
访问,在这个例子中我只是打印与其关联的 DOM 节点。 - Nikhil Fadnis<Child>
内部的组件,我该如何从父组件中引用它? - pmiranda这是一个以ref为重点的输入示例(在React 16.8.6中测试通过):
子组件:
class Child extends React.Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
}
render() {
return (<input type="text" ref={this.myRef} />);
}
}
父组件内包含子组件:
class Parent extends React.Component {
constructor(props) {
super(props);
this.childRef = React.createRef();
}
componentDidMount() {
this.childRef.current.myRef.current.focus();
}
render() {
return <Child ref={this.childRef} />;
}
}
ReactDOM.render(
<Parent />,
document.getElementById('container')
);
使用this.props.children的父组件:
class Parent extends React.Component {
constructor(props) {
super(props);
this.childRef = React.createRef();
}
componentDidMount() {
this.childRef.current.myRef.current.focus();
}
render() {
const ChildComponentWithRef = React.forwardRef((props, ref) =>
React.cloneElement(this.props.children, {
...props,
ref
})
);
return <ChildComponentWithRef ref={this.childRef} />
}
}
ReactDOM.render(
<Parent>
<Child />
</Parent>,
document.getElementById('container')
);
this.props.children
是什么类型? - doug65536首先通过this.props.children
访问子元素,每个子元素将会有它的ref
作为属性。
ref
属性为 null
。 - qwertzguy如果你的所有东西都是props.children
:
const Parent = (p: {children: JSX.Element}) => {
const childRef = useRef()
return React.cloneElement(p.children, { ref: childRef })
}
<Parent>
<SingleChild />
</Parent>
ref
,例如 React.Fragment
,则此方法将失败。const FancyButton = React.forwardRef((props, ref) => (
<button ref={ref} className="FancyButton">
{props.children}
</button>
));
// You can now get a ref directly to the DOM button:
const ref = React.createRef();
<FancyButton ref={ref}>Click me!</FancyButton>;
注意 :只有使用 React.forwardRef 调用定义组件时才存在第二个 ref 参数。普通函数式或类组件不接收 ref 参数,而在 props 中也不可用。
Ref 转发不仅限于 DOM 组件。您还可以将 Ref 转发到类组件实例中。
参考资料:React 文档。
我将展示如何解决动态组件的问题:
在父组件上,动态地创建对子组件的引用,例如:
class Form extends Component {
fieldRefs: [];
// dynamically create the child references on mount/init
componentWillMount = () => {
this.fieldRefs = [];
for(let f of this.props.children) {
if (f && f.type.name == 'FormField') {
f.ref = createRef();
this.fieldRefs.push(f);
}
}
}
// used later to retrieve values of the dynamic children refs
public getFields = () => {
let data = {};
for(let r of this.fieldRefs) {
let f = r.ref.current;
data[f.props.id] = f.field.current.value;
}
return data;
}
}
子组件(即 <FormField />)实现了自己的“field”引用,可以从父组件中引用:
class FormField extends Component {
field = createRef();
render() {
return(
<input ref={this.field} type={type} />
);
}
}
然后,在你的主页面,也就是“父亲的父亲”组件中,你可以通过引用获取字段值,如下:
class Page extends Component {
form = createRef();
onSubmit = () => {
let fields = this.form.current.getFields();
}
render() {
return (
<Form ref={this.form}>
<FormField id="email" type="email" autoComplete="email" label="E-mail" />
<FormField id="password" type="password" autoComplete="password" label="Password" />
<div class="button" onClick={this.onSubmit}>Submit</div>
</Form>
);
}
}
class Field extends Component {
const { inputRef } = this.props;
render() {
return (
<input type="text" ref={inputRef} />
)
}
}
class MyComponent extends Component {
componentDidMount() {
this.inputNode.focus();
}
render() {
return (
<div>
Hello, <Field inputRef={node => this.inputNode = node} />
</div>
)
}
}
如果您想在父组件中获得子组件的引用
// children is an array
const updatedChildren = Children.map(children, (child) => {
return { ...child.props, ref: child.ref };
}),
// children is not an array
const childrenRef = children.ref;