我希望有人能指出我在useRef下面的组件中进行测试的正确方向。
我有一个结构如下的组件。我正在尝试测试其中的功能,otherFunction()但我不确定如何模拟来自组件引用的当前属性。有没有人做过这样的事情?
const Component = (props) => {
const thisComponent = useRef(null);
const otherFunction = ({ current, previousSibling }) => {
if (previousSibling) return previousSibling.focus();
if (!previousSibling && current) return current.focus();
}
const handleFocus = () => {
const {current} = thisComponent;
otherFunction(current);
}
return (
<div ref={thisComponent} onFocus={handleFocus}>Stuff In here</div>
);
};
Run Code Online (Sandbox Code Playgroud)