我正在用来ignite创建一个expo react native应用程序。我正在使用本指南https://ignitecookbook.com/docs/recipes/GeneratorComponentTests创建组件测试文件
这是第一个测试文件
\n\nimport React from "react"\nimport { fireEvent, render, screen } from "@testing-library/react-native"\nimport { HelloWorld } from "../HelloWorld"\n\ndescribe("HelloWorld", () => {\n it("renders", () => {\n render(<HelloWorld />)\n expect(screen.getByText("Hello")).toBeTruthy()\n })\n})\nRun Code Online (Sandbox Code Playgroud)\n我按照https://reactnativetesting.io/component/setup/进行安装@testing-library/react-native,但现在我得到了
\xe2\x97\x8f Test suite failed to run\n\n Jest encountered an unexpected token\n\n Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is …Run Code Online (Sandbox Code Playgroud) reactjs jestjs react-native expo react-native-testing-library
import { useState } from "react";
export default function App() {
const [buttonClicked, setButtonClicked] = useState(false);
console.log('Render');
return (
<div className="App">
<button onClick={() => setButtonClicked(true)}>Click me</button>
{buttonClicked && <div>Button was clicked</div>}
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
该组件未在StrictMode. 它首先渲染,因此我们在控制台中看到一个渲染。当我们单击按钮时,它会因状态更新而重新呈现。我们可以Render再次在控制台看到。但是,令我惊讶的是,当我们再次单击该按钮时,它也会重新渲染该组件,我们Render第三次在控制台中看到。
我认为状态更新正在检查值是否已更改,因此不会第三次渲染。
对我来说更奇怪的是,当我们第三次单击该按钮时,它不会重新渲染组件。
为什么会发生这种情况?
Codesandbox: https://codesandbox.io/s/proud-star-rg49x9? file=/src/App.js:0-336
reactjs ×2
expo ×1
jestjs ×1
react-hooks ×1
react-native ×1
react-native-testing-library ×1
react-state ×1
rendering ×1