小编Str*_*oop的帖子

useRef 钩子不会触发重新渲染

出于某种原因,我的测试在使用这样的 ref 时没有通过:

import React, { useRef } from "react";
import { waitForElement, render } from "@testing-library/react";

const MyComponent = props => {
  const elementRef = useRef(null);
  return (
    <div>
      <div ref={elementRef} />
      {elementRef.current && <div data-testid="test-div" />}
    </div>
  );
};

describe("MyComponent", () => {
  it("test-div should exist when the ref is set", async done => {
    const { getByTestId } = render(<MyComponent />);
    await waitForElement(() => getByTestId("test-div"));
  });
});
Run Code Online (Sandbox Code Playgroud)

知道为什么这不起作用吗?

reactjs react-hooks

6
推荐指数
1
解决办法
8337
查看次数

标签 统计

react-hooks ×1

reactjs ×1