- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要访问子组件的位置。据我了解,要访问子属性,我需要使用 useImperativeHandle
将子 API 添加到其 ref。此外,我需要使用 forwardRef
将引用从父级传递给子级。所以我这样做了:
const Text = React.forwardRef(({ onClick }, ref) => {
const componentAPI = {};
componentAPI.getLocation = () => {
return ref.current.getBoundingClientRect ? ref.current.getBoundingClientRect() : 'nope'
};
React.useImperativeHandle(ref, () => componentAPI);
return (<button onClick={onClick} ref={ref}>Press Me</button>);
});
Text.displayName = "Text";
const App = () => {
const ref = React.createRef();
const [value, setValue] = React.useState(null)
return (<div>
<Text onClick={() => setValue(ref.current.getLocation())} ref={ref} />
<div>Value: {JSON.stringify(value)}</div>
</div>);
};
ReactDOM.render(<App />, document.querySelector("#app"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="app"></div>
如您所见,ref 没有 getBoundingClientRect
属性,但如果我这样做,它将按预期工作:
const App = () => {
const ref = React.createRef();
const [value, setValue] = React.useState(null)
return (<div>
<button ref={ref} onClick={() => setValue(ref.current.getBoundingClientRect()) } ref={ref}>Press Me</button>
<div>Value: {JSON.stringify(value)}</div>
</div>);
};
ReactDOM.render(<App />, document.querySelector("#app"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="app"></div>
那么我对useImperativeHanedle
和forwardRef
的理解有什么问题吗?
最佳答案
要使用 useImperativeHandle
,您需要像这样使用另一个 ref
实例:
const Text = React.forwardRef(({ onClick }, ref) => {
const buttonRef = React.useRef();
React.useImperativeHandle(
ref,
() => ({
getLocation: () => buttonRef.current.getBoundingClientRect()
}),
[buttonRef]
);
return (
<button onClick={onClick} ref={buttonRef}>
Press Me
</button>
);
});
如果您希望您的逻辑有效(使用相同的转发ref
),这将起作用:
const Text = React.forwardRef(({ onClick }, ref) => {
React.useEffect(() => {
ref.current.getLocation = ref.current.getBoundingClientRect;
}, [ref]);
return (
<button onClick={onClick} ref={ref}>
Press Me
</button>
);
});
为什么您的示例不起作用?
因为 ref.current.getBoundingClientRect
在 useImperativeHandle
中分配它时不可用(尝试记录它)因为你实际上覆盖了按钮的 ref
with useImperativeHandle
(检查沙箱中的Text3
,ref.current
值在挂载后分配了getLocation
) .
关于javascript - 正在设置 React useImperativeHandle 和 forwardRef,引用似乎没有更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60599895/
我有一个带有保存按钮的组件 requestSaveAsync()}>Save 我还需要能够从应用程序的顶层调用 requestSaveAsync,因此我在名为 useEditRow 的自定义钩子(H
我在子组件的 useImperativeHandle Hook 中创建了一个方法。然后,我可以完美地从父组件访问它。就像下面的例子。但是,我无法从子组件访问它。换句话说,focus from pare
我在子组件的 useImperativeHandle Hook 中创建了一个方法。然后,我可以完美地从父组件访问它。就像下面的例子。但是,我无法从子组件访问它。换句话说,focus from pare
function App(){ const cntEl:any = React.useRef(null); // I don't know what type should be here.
我需要使用 ref获取孙组件的状态,我使用 useImperativeHandle 对其进行了自定义,简化了整个代码 here . function App() { const ref = use
我不明白为什么需要以下 useImperativeHandle、useLayoutEffect 和 useDebugValue 钩子(Hook),您能否举例说明何时可以使用它们,但请不要使用文档中的示
我需要访问子组件的位置。据我了解,要访问子属性,我需要使用 useImperativeHandle 将子 API 添加到其 ref。此外,我需要使用 forwardRef 将引用从父级传递给子级。所以
文档中提供的示例如下: function FancyInput(props, ref) { const inputRef = useRef(); useImperativeHandle(ref
我是一名优秀的程序员,十分优秀!