html - 如何为反应组件提供 Id 或 Attribute
问题描述
我们需要为我们的 react 组件提供一些标识符,以便我们可以在这些控件上编写自动化测试用例。
import React from 'react';
import ReactDOM from 'react-dom';
import { Grid, GridColumn as Column } from '@progress/kendo-react-grid';
import products from './products.json';
class App extends React.Component {
state = {
gridData: products
}
render() {
return (
<div>
<Grid
data-grid="myGrid"
style={{ height: '400px' }}
data={this.state.gridData}
>
<Column field="ProductID" title="ID" width="40px" />
<Column field="ProductName" title="Name" width="200px" />
<Column field="Category.CategoryName" title="CategoryName" />
<Column field="UnitPrice" title="Price" width="80px" />
<Column field="UnitsInStock" title="In stock" width="80px" />
<Column field="Discontinued" width="120px"
cell={(props) => (
<td>
<input disabled type="checkbox" checked={props.dataItem[props.field]} />
</td>
)} />
</Grid>
</div>
);
}
}
ReactDOM.render(
<App />,
document.querySelector('my-app')
);
我们试图为网格提供data-grid="myGrid"属性,但这似乎不起作用。您能否建议这里的错误是什么或解决方案是什么。
解决方案
你应该使用 prop 传递camelCase
它应该是这样的:
<Grid dataGrid="myGrid" />
在里面Grid
你可以像这样指定它:
const Grid = ({dataGrid}) => (
<div data-grid={dataGrid} />
)
或者您可以设置id
为组件<Grid id="myGrid" />
推荐阅读
- javascript - 使用调度更改 obj 的特定值
- cmd - 使用 CLI 创建 AWS DynamoDB 表时出错
- javascript - 当我编辑我的 css 文件并刷新我的页面时,没有出现更改
- karate - 空手道 - 如何将 Junit RunListener 添加到 KarateParallel Runner
- c# - msbuild步骤生成c#文件然后编译
- c++ - 已知数组/指针大小作为参数
- c# - C#基本的hello world没有运行
- bash - 强制更改 Docker 映像中的时区
- ruby-on-rails - 对何时将 rails 版本从 5.0.2 更新到 n 6.0.2.1 感到困惑
- git - 如何使用 github PR 工作流更好地针对分布式系统进行端到端测试?