小编Div*_*vya的帖子

React Ref 的 offsetheight,即使在加载内容后,对于玩笑测试用例也始终返回“0”

我正在使用react-testing-libraryjest。在 componentdidupdate 中尝试获取“ this.testRef.current.offsetHeight ”,即使在加载内容后,offsetHeight 仍为0 。我想要 DOM 的精确 offsetHeight 因为有一些基于它的条件。

在反应测试库和笑话中是否有任何可能的解决方案可以解决。

我正在使用angerouslySetInnerHTML来绑定testRef 元素的html 内容。

dom reactjs jestjs react-testing-library

14
推荐指数
1
解决办法
1万
查看次数

使用 react usestate hook 防止为每个 setstate 重新渲染

我有一些多个状态和函数来使用 useState 钩子设置状态。

在每个 setstate 组件上重新渲染。我只想重新渲染完全完成的 setstate 功能。

例如:

在 app.js 中

const [a, setA] = useState(() => false);
const [b, setB] = useState(() => {});
const [c, setC] = useState(() => props.c);
const [d, setD] = useState(null);
const [e, setE] = useState(null);
const [f, setF] = useState(null);
const [g, setG] = useState('');

const func1 = useCallback(data => {
  setA(true);
  setB(true);
  setC(data && data.c);
  setD();
  setE(isChecked ? 'a' : 'b');
}, []);

const func2 = useCallback(data => {
  setA(false);
  setB(false); …
Run Code Online (Sandbox Code Playgroud)

reactjs react-hooks

5
推荐指数
1
解决办法
1万
查看次数