小编myc*_*OOM的帖子

React 测试库 - 在测试组件之前等待状态更新

我正在第一次渲染时测试组合框,组合框的初始值未定义。然后使用查询参数和 useEffect 组件找到相关选项并为其设置值状态。


const Component = () => {

[value, setValue] = useState(undefined);

useEffect(() => {

setValue("hello")

}, [])


return(
<Combobox value={value}/>
)

}

Run Code Online (Sandbox Code Playgroud)
it("should render with value as hello", () => {

const {getByText} = render(<Component/>)

const text = getByText("hello")

})

Run Code Online (Sandbox Code Playgroud)

该测试套件抛出此错误:

TestingLibraryElementError: Unable to find an element with the text: Photobug. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-testing-library react-hooks react-state

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

React router v6 访问路由外参数的最简单方法

<Menu/>

<Routes>
<Route path="/" element={<HomeView />} />
<Route path="/website/:Id" element={<WebsiteView />} />
</Routes>
Run Code Online (Sandbox Code Playgroud)

从 Menu 组件访问 Id 参数的最简单方法是什么?

UseParams 只能在路由渲染的元素中使用。

我目前正在这样做:

const location = useLocation();

const Id = location.pathname.split("/")[2]
Run Code Online (Sandbox Code Playgroud)

然而,我认为这对我来说还不够严格。我想要用反应方式检索 Id Param - 请不要花哨的解决方案,这是最简单的方法。

reactjs react-router

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