Reactjs 为什么toggleSelection和isSelected方法接收不同的关键参数?
我正在使用6.10.0版中的react表。用打字稿。 使用hoc/selectTable可以轻松添加复选框 但是,toggleSelection您需要提供的用于管理选择的isSelected方法正在接收不同的密钥。 toggleSelection方法在开始时接收额外的“select-” 我找不到任何能说明这一问题的例子 我知道这个问题有一个简单的解决方法,但是我仍然找不到任何一个在开始时需要额外字符串的例子。我是个新手,似乎做得不对Reactjs 为什么toggleSelection和isSelected方法接收不同的关键参数?,reactjs,react-table,Reactjs,React Table,我正在使用6.10.0版中的react表。用打字稿。 使用hoc/selectTable可以轻松添加复选框 但是,toggleSelection您需要提供的用于管理选择的isSelected方法正在接收不同的密钥。 toggleSelection方法在开始时接收额外的“select-” 我找不到任何能说明这一问题的例子 我知道这个问题有一个简单的解决方法,但是我仍然找不到任何一个在开始时需要额外字符串的例子。我是个新手,似乎做得不对 import "bootstrap/dist/css/boot
import "bootstrap/dist/css/bootstrap.min.css";
import ReactTable, { RowInfo } from "react-table";
import "react-table/react-table.css";
import checkboxHOC, { SelectType } from "react-table/lib/hoc/selectTable";
const CheckboxTable = checkboxHOC(ReactTable);
....
render() {
...
<CheckboxTable
data={this.getData()}
columns={this.columnDefinitions()}
multiSort={false}
toggleSelection={(r,t,v) => this.toggleSelection(r,t,v)}
isSelected={(key)=> this.isSelected(key)}
/>
}
...
toggleSelection = (key: string, shiftKeyPressed: boolean, row: any): any => {
...
//implementation -over here key is always like "select-" + _id
...}
isSelected = (key: string): boolean => {
// key received here is only _id
return this.state.selection.includes(key);
}
导入“bootstrap/dist/css/bootstrap.min.css”;
从“react table”导入ReactTable,{RowInfo};
导入“react table/react table.css”;
从“react table/lib/hoc/selectTable”导入checkboxHOC,{SelectType};
const CheckboxTable=checkboxHOC(ReactTable);
....
render(){
...
this.toggleSelection(r,t,v)}
isSelected={(键)=>this.isSelected(键)}
/>
}
...
toggleSelection=(键:string,shiftKeyPressed:boolean,行:any):any=>{
...
//实现-此处的键始终类似于“选择-”+\u id
...}
isSelected=(键:字符串):布尔=>{
//此处收到的密钥仅为_id
返回此.state.selection.includes(键);
}
在我看到的所有示例中,这些方法都是使用相同的键提供的。查看,它似乎按照预期工作,或者存在错误。如果你没有发现任何其他提到这一点的地方,很可能是前者
这是创建SelectInputComponents的地方:
rowSelector(row) {
if (!row || !row.hasOwnProperty(this.props.keyField)) return null
const { toggleSelection, selectType, keyField } = this.props
const checked = this.props.isSelected(row[this.props.keyField])
const inputProps = {
checked,
onClick: toggleSelection,
selectType,
row,
id: `select-${row[keyField]}`
}
return React.createElement(this.props.SelectInputComponent, inputProps)
}
感兴趣的两个处理程序是onClick(映射到toggleSelection)和checked(映射到isSelected)。注意这里的id
SelectInputComponent如下所示:
const defaultSelectInputComponent = props => {
return (
<input
type={props.selectType || 'checkbox'}
aria-label={`${props.checked ? 'Un-select':'Select'} row with id:${props.id}` }
checked={props.checked}
id={props.id}
onClick={e => {
const { shiftKey } = e
e.stopPropagation()
props.onClick(props.id, shiftKey, props.row)
}}
onChange={() => {}}
/>
)
const defaultSelectInputComponent=props=>{
返回(
{
常数{shiftKey}=e
e、 停止传播()
props.onClick(props.id、shiftKey、props.row)
}}
onChange={()=>{}
/>
)
在onClick(即toggleSelection)处理程序中,可以看到props.id作为第一个参数传入。因此,这就是添加附加select-
的地方
我不熟悉这个包,所以我不能告诉您这是一个bug还是一个特性,但是这些回调参数的传递方式是不同的。由于软件包的成熟性,我强烈建议这是预期行为。是否在某处明确设置了键?我没有设置任何键,至少没有直接设置。感谢您的调查。官方的反应表示例:让我思考为什么键没有区别