Forwarding Refs
This feature allows you to forward refs to DOM elements or other components. In this example, the `Ref` component is used to forward the `inputRef` to the `input` element.
import { Ref } from '@fluentui/react-component-ref';
import React, { useRef } from 'react';
const MyComponent = () => {
const inputRef = useRef(null);
return (
<Ref innerRef={inputRef}>
<input type="text" />
</Ref>
);
};
Handling Complex Component Hierarchies
This feature is useful for managing refs in complex component hierarchies. In this example, `Ref` is used to forward the `divRef` to the `InnerComponent`, which in turn forwards it to the `div` element.
import { Ref } from '@fluentui/react-component-ref';
import React, { useRef } from 'react';
const InnerComponent = React.forwardRef((props, ref) => (
<div ref={ref} {...props} />
));
const OuterComponent = () => {
const divRef = useRef(null);
return (
<Ref innerRef={divRef}>
<InnerComponent />
</Ref>
);
};