Reputation: 69
i want to outsouring the available props from my react table. I have search some websites, but i don't find anything about this case. The background of this case is, that i want to use this table more than once and i don't want to initialize the table again and again.
<ReactTable
className='-striped -highlight mb-3'
filterable
data={this.state.data}
loading={this.state.loading}
sortable={true}
multiSort={true}
showPageJump={true}
showPagination={false}
showPageSizeOptions={true}
collapseOnDataChange={true}
collapseOnPageChange={true}
collapseOnSortingChange={true}
freezeWhenExpanded={true}
resizable={false}
pageSize={this.state.data.length}
filtered={this.state.filtered}
onFilteredChange={filtered => this.setState({ filtered })}
columns={this.state.columns}
/>
From this code i want to outsource the follwing props:
Have you any idea, how i outsource this?
Regards MCW
Upvotes: 0
Views: 2893
Reputation: 62881
Sounds like you're looking for Higher Order Components (HOCs) - a technique in React for reusing component logic. HOCs are not part of the React API, but instead a pattern that emerges from React’s compositional nature.
In this case, you want to create a HOC using some common props
. One option to accomplish this is using the ReCompose library, with it's withProps
method. Here's an example:
import React, { Component } from "react";
import ReactDOM from "react-dom";
import { withProps } from "recompose";
import ReactTable from "react-table";
const CommonTable = withProps({
className: '-striped -highlight mb-3',
multisort: true,
showPagination: false,
resizable: false,
etc: etc
})(ReactTable);
Also, it looks like there are some factors to be aware of when using ReactTable with HOC, outlined in the docs.
Here's an example:
const { withProps } = Recompose;
const CommonTable = withProps({
className: '-striped -highlight mb-3',
multisort: true,
showPagination: false,
resizable: false
})(ReactTable.default);
const data = [
{
name: 'Tanner Linsley',
age: 26,
friend: {
name: 'Jason Maurer',
age: 23,
}
}
];
const columns = [
{
Header: 'Name',
accessor: 'name' // String-based value accessors!
}, {
Header: 'Age',
accessor: 'age',
Cell: props => <span className='number'>{props.value}</span> // Custom cell components!
}, {
id: 'friendName', // Required because our accessor is not a string
Header: 'Friend Name',
accessor: d => d.friend.name // Custom value accessors!
}, {
Header: props => <span>Friend Age</span>, // Custom header components!
accessor: 'friend.age'
}
];
ReactDOM.render(<CommonTable data={data} columns={columns} />, document.getElementById("app"));
<link href="https://cdnjs.cloudflare.com/ajax/libs/react-table/6.7.6/react-table.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/recompose/0.26.0/Recompose.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-table/6.7.6/react-table.js"></script>
<div id="app"></div>
Upvotes: 2