在表列标题中,我想返回导航到每个标题的编辑模式的链接。我使用了react-bootstrap-table,并在组件的构造函数中创建了一个自定义数据格式化程序
class Grid extends Component {
constructor(props) {
super(props);
this.anchorFormatter = (cell, row, slug) => {
let link = "/"+slug;
return (
<Link to={link}>
{cell}
</Link>
)
}然后,我将该数据称为表中格式化的数据
<TableHeaderColumn isKey dataField="title" dataSort dataFormat={ this.anchorFormatter }>Title</TableHeaderColumn>这是获取此错误
Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
Check the render method of `TableBody`.第二部分是如何将slug的值传递给数据格式化程序?我从get请求API调用中获得了这样的数据
{
"title": "Experiments in DataOps",
"status": true,
"publish_date": "2020-01-29",
"slug": "experiments-in-dataops"
},发布于 2020-02-24 22:13:58
另一种方法是创建一个类或函数,然后将其用作组件,在这里我将创建组件
class BlogAnchor extends Component {
constructor(props) {
super(props);
}
render() {
return (
<Link to={this.props.link}> {this.props.linkText} </Link>
);
}
}然后在Grid类中,我有一个函数
anchorFormatter(cell, row) {
let link = "blogs/" + row.slug;
return <BlogAnchor link={link} linkText={cell} />;
}它也在Grid类的构造函数中注册
this.anchorFormatter = this.anchorFormatter.bind(this);表列引用了this this函数
<TableHeaderColumn isKey dataField="title" dataSort dataFormat {this.anchorFormatter}>Title</TableHeaderColumn>发布于 2020-02-13 17:44:07
我不得不使用原始的html标签而不是链接才能正常工作。
const anchorFormat = (cell,row) => {
let link = '#/blogs/' + row.slug;
return ( <div><a className="nav-link" href={link}>{cell}</a></div>);
}https://stackoverflow.com/questions/60023753
复制相似问题