SlideShare a Scribd company logo
React Table
Tutorial:
Project Setup,
useTable, and
useFilter
www.bacancytechnology.com
Introduction
Table user interfaces are ubiquitous, mostly
used, and organized UI preferred by users
and developers. It makes the data look
simple and easily accessible. Being a ReactJS
developer, you might have heard of React
Table V7; few might have implemented it
too. If you are looking for a tutorial
explaining React Table V7 with an Example,
you have chosen the right blog. Today we
are here with a react-table tutorial for you.


I understand how challenging it could be
when you are trying to learn something
new, but I also know how interesting is that!
Isn’t it?


Refer to the next section that includes the
points covered in this tutorial – React Table
Tutorial – Project Setup, Installation,
useTable, and useFilter.
React Table
Tutorial Goal:
useTable and
useFilter
Before starting the development process
let’s have a look at the below video so that
you can understand what are we building.




Video: https://youtu.be/5rXI5E9iCkE
React Table
v7
The creator of React Table – Tanner Linsley,
launched React Table v7 in March 2020. We
might remember using the class component
of react-table, but now the library provides
the Hooks-based APIs and plugins for
creating a hassle-free React Table. The
release is considered a significant change as
the approach to creating a table, table UI,
and style has changed.
New Features in React
Table v7


Headless (100% customizable, Bring-
your-own-UI)
Lightweight (5kb – 14kb+ depending on
features used and tree-shaking)
Sorting (Multi and Stable)
Filters
Animatable
Row Expansion
Column Ordering
Virtualizable
Server-side/controlled data/state
Auto out of the box, fully controllable
API
Considering React Table release note, here
are the new features in React Table v7:
Extensible via a hook-based plugin
system
Row Selection
Resizable
Pivoting & Aggregation
Project
Set-Up
Create ReactJS project using the below
command.


npx create-react-app react-table-demo
Install
react-table
and Axios
Install react-table and axios.


npm install react-table axios --save //npm
yarn add react-table axios //yarn
Looking for proficient
ReactJs developers for your
project?
Bacancy has the best developers with
extraordinary problem-solving skills and
Javascript knowledge. Get in touch with us
today and hire ReactJS developer to build
your dream product.
Getting
started with
React Table
Example
App.js – main file
TableContainer.js – having a Table
component.


After done with project setup and
installation, follow these steps to
implement React Table Example. I’ll be
writing the entire code in two files, i.e.,
Importing Axios and Hooks
import React, { useState, useEffect,
useMemo } from "react";
import axios from "axios";
Initializing state using
useState


const [data, setData] = useState([]);
Fetching Data using Axios


useEffect(() => {
axios("http://api.tvmaze.com/search/shows
?q=girls")
.then((res) => {
setData(res.data);
})
.catch((err) => console.log(err))
}, []);
I have called
“http://api.tvmaze.com/search/shows?
q=girls“
If the promise is resolved, it will execute
then block, in which we will store the
response in the state using
setData(res.data)
And if the promise is rejected, it will
execute the catch block and console the
error.
Defining Columns


Header – the name of the column
Accessor – key in data.
After preparing our data, let’s define the
columns of the Table. The column structure
would consist-


We will wrap it inside hook useMemo as far
as the optimization is concerned.
const columns = useMemo(
() => [
{
Header: "TV Show",
columns: [
{
Header: "Name",
accessor: "show.name"
},
{
Header: "Type",
accessor: "show.type"
},
{
Header: "Language",
accessor: "show.language"
},
{
Header: "Official Site",
accessor: "show.officialSite",
Cell: ({ cell: { value } }) => value ? {value}
: "-"
},
{
Header: "Rating",
accessor: "show.rating.average",
Cell: ({ cell: { value } }) => value || "-"
},
{
Header: "Status",
accessor: "show.status",
},
{
Header: "Premiered",
accessor: "show.premiered",
Cell: ({ cell: { value } }) => value || "-"
},
{
Header: "Time",
accessor: "show.schedule.time",
Cell: ({ cell: { value } }) => value || "-"
},
]
}
]
)
You might be wondering why I have written
“show.name”, “show.type”,
“show.rating.average” and so on. It is
because the data is inside the show object,
and for accessing the data, we will use show.
as the prefix. Here is the sample of data-
Custom Cell


{
Header: " Official Site ",
accessor: " show.officialSite ",
Cell: ( props ) => {
return < YourComponent { ...props } / >
}
},


We can have the custom cell for each row as
shown above. A cell has access to the values
of each row; you can console props to see
what it consists of.


Our demo will implement the custom cell to
check whether show.officalSite has the
value or not. If it has the value then it will
return or “-”
{
Header: "Official Site",
accessor: "show.officialSite",
Cell: ({ cell: { value } }) => value ?
{value} : "-"
},
Implement
useTable
Hook in React
Table Tutorial
Data consists of the data of the API
response
Columns is an array of objects for
defining table columns.


We will create another file named –
TableContainer.js in which we will build our
Table component using the useTable hook.


It will take two properties: data and
columns, which we have defined in the
above sections.
import React from "react";
import { useTable } from "react-table";
export default function Table({ columns,
data }) {
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
} = useTable({
columns,
data,
})
return (
< table {...getTableProps()} >
< head >
{headerGroups.map(headerGroup => (
< tr
{...headerGroup.getHeaderGroupProps()} >
{headerGroup.headers.map(column =
> (
< th {...column.getHeaderProps()}>
{column.render('Header')}< /th >
))}
))}
< /thead >
< tbody {...getTableBodyProps()} >
{rows.map((row, i) => {
prepareRow(row)
return (
< tr {...row.getRowProps()} >
{row.cells.map(cell => {
return < td {...cell.getCellProps()}>
{cell.render('Cell')}< /td >
})}
< /tr >
)
})}
< /tbody >
< /table >
)
}
Rendering
React Table
Import the Table from the TableContainer.js
and then render on the UI using


import Table from './TableContainer'
< div className="App" >
< h1 >< center >React Table Demo<
/center >< /h1 >
< Table columns={columns} data={data} />
< /div >
After implementing the above code
snippets, following your App.js and
TableContainer.js will look like this –


// App.js


import React, { useState, useEffect,
useMemo } from "react";
import axios from "axios";
import { useTable } from "react-table";
import './App.css';
function Table({ columns, data }) {
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
} = useTable({
columns,
data,
})
return (
< table {...getTableProps()} >
< head >
{headerGroups.map(headerGroup => (
< tr
{...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column =>
(
< th {...column.getHeaderProps()}>
{column.render('Header')}< /th >
))}
< /tr >
))}
< /thead >
< tbody {...getTableBodyProps()}>
{rows.map((row, i) => {
prepareRow(row)
return (
< tr {...row.getRowProps()}>
{row.cells.map(cell => {
return {cell.render('Cell')}
})}
< /tr >
)
})}
< /tbody >
< /table >
)
}
function App() {
const [data, setData] = useState([]);
useEffect(() => {
axios("http://api.tvmaze.com/search/shows
?q=girls")
.then((res) => {
setData(res.data);
})
.catch((err) => console.log(err))
}, []);
const columns = useMemo(
() => [
{
Header: "TV Show",
columns: [
{
Header: "Name",
accessor: "show.name"
},
{
Header: "Type",
accessor: "show.type"
},
{
Header: "Language",
accessor: "show.language"
},
{
Header: "Official Site",
accessor: "show.officialSite",
Cell: ({ cell: { value } }) => value ? {value}
: "-"
},
{
Header: "Rating",
accessor: "show.rating.average",
Cell: ({ cell: { value } }) => value || "-"
},
{
Header: "Status",
accessor: "show.status",
},
{
Header: "Premiered",
accessor: "show.premiered",
Cell: ({ cell: { value } }) => value || "-"
},
{
Header: "Time",
accessor: "show.schedule.time",
Cell: ({ cell: { value } }) => value || "-"
},
]
}
]
)
return (
< div className="App" >
< h1 >< center >React Table Demo<
/center >< /h1 >
< Table columns={columns} data={data} /
>
< /div >
);
}
export default App;
// TableContainer.js


import React from "react";
import { useTable } from "react-table";
export default function Table({ columns,
data }) {
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
} = useTable({
columns,
data,
})
return (
< table {...getTableProps()}>
< head >
{headerGroups.map(headerGroup => (
< tr
{...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column =>
(
< th {...column.getHeaderProps()}>
{column.render('Header')}
))}
< /tr >
))}
< /thead >
< tbody {...getTableBodyProps()} >
{rows.map((row, i) => {
prepareRow(row)
return (
< tr {...row.getRowProps()}>
{row.cells.map(cell => {
return {cell.render('Cell')}
})}
< /tr >
)
})}
< /tbody >
< /table >
)
}
After running the command npm run start
you will see something like this-
Implement
useFilter
Hook
Now, moving on to useFilter and
useGlobalFilter in our application.
According to our UI, this will be our project
structure.
Update App.js
TableContainer.js
We will implement the default filter and
select column filter. For that, we will
Create a new file named – Filter.js (which
will have functional components for Filter
views)


Without further ado, let’s create Filter.js
and define React table components for
Filter UI.
Defining
Filter
Component
for UI
Default Filter for Columns: It will render
text input, and the column data is
filtered based on the text entered.
Global Filter: It will render text input
but not just for columns; the entire
table data is filtered based on the text
entered.
Select Filter for Column: It will render
select input, and the column data is
filtered based on the option selected
from the list.


In this React Table demo, we will
implement three filter views –
We will create a common file named Filter.js
(or with any suitable name) from where we
will export the above-mentioned functional
components for readability purposes.


// Filter.js


import { React, useMemo, useState } from
"react";
import { useAsyncDebounce } from "react-
table";
import { Label, Input } from "reactstrap";
// Component for Global Filter
export function GlobalFilter({
globalFilter,
setGlobalFilter
}) {
const [value, setValue] =
useState(globalFilter);
const onChange =
useAsyncDebounce((value) => {
setGlobalFilter(value || undefined);
}, 200);
return (
<div>
<Label>Search Table: </Label>
<Input
value={value || ""}
onChange={(e) => {
setValue(e.target.value);
onChange(e.target.value);
}}
placeholder=" Enter value "
className="w-25"
style={{
fontSize: "1.1rem",
margin: "15px",
display: "inline",
}}
/>
</div>
);
}
// Component for Default Column Filter
export function DefaultFilterForColumn({
column: {
filterValue,
preFilteredRows: { length },
setFilter,
},
}) {
return (
<Input
value={filterValue || ""}
onChange={(e) => {
// Set undefined to remove the filter
entirely
setFilter(e.target.value || undefined);
}}
placeholder={`Search ${length} records..`}
style={{ marginTop: "10px" }}
/>
);
}
// Component for Custom Select Filter
export function SelectColumnFilter({
column: { filterValue, setFilter,
preFilteredRows, id },
}) {
// Use preFilteredRows to calculate the
options
const options = useMemo(() => {
const options = new Set();
preFilteredRows.forEach((row) => {
options.add(row.values[id]);
});
return [...options.values()];
}, [id, preFilteredRows]);
// UI for Multi-Select box
return (
<select
value={filterValue}
onChange={(e) => {
setFilter(e.target.value || undefined);
}}
>
<option value="">All</option>
{options.map((option, i) => (
<option key={i} value={option}>
{option}
</option>
))}
</select>
);
}
Explanation
What is the use of useAsyncDebounce?


– React table provides useAsyncDebounce
to avoid the multiple re-renders caused due
to side-effects and to use the latest one.
Back-to-back state side-effects take place
that triggers multiple renders. So, rather
than handling it manually, React Table
provides a hook to debounce the rapid side-
effects.


Here, we have little data, so that we won’t
realize the importance of
useAsyncDebounce. But, consider, if we
have thousands of data filtered, then the
continuous state changes and side-effects
are much costlier than this demo app.


A good developer takes care of the
performance even while coding for a demo
application. Try avoiding trash coding.
The setfilter, filterValue, and
preFilterRows are column properties
used for specific columns. We have
destructured the column prop and used
them to get the filter values of
respective columns.
Rendering
Filter
Component
For GlobalFilter and
DefaultFilterForColumn


Open TableContainer.js and Import
components and hooks-


import { useTable, useFilters,
useGlobalFilter } from "react-table";
import { GlobalFilter,
DefaultFilterForColumn} from "./Filter";
Pass DefaultFilterForColumn to useTable
hook as a default filter for all the columns.
So by default, your columns will have these
components as filters unless you provide a
custom filter or disable the filter.


useTable(
{
columns,
data,
defaultColumn: { Filter:
DefaultFilterForColumn },
},
useFilters,
useGlobalFilter
Now for rendering the UI, use the following
code-


{/* Rendering Global Filter */}
{/* Rendering Default Column Filter */}
{column.canFilter ? column.render("Filter")
: null}
Your entire TableContainer.js will look like
this-


// TableContainer.js


import { React } from "react";
import { useTable, useFilters,
useGlobalFilter } from "react-table";
import { GlobalFilter, DefaultColumnFilter }
from "./Filter";
export default function Table({ columns,
data }) {
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
state,
visibleColumns,
prepareRow,
setGlobalFilter,
preGlobalFilteredRows,
} = useTable(
{
columns,
data,
defaultColumn: { Filter:
DefaultFilterForColumn },
},
useFilters,
useGlobalFilter
);
return (
<table {...getTableProps()}>
<thead>
<tr>
<th
colSpan={visibleColumns.length}
style={{
textAlign: "center",
}}
>
{/* Rendering Global Filter */}
<GlobalFilter
preGlobalFilteredRows=
{preGlobalFilteredRows}
globalFilter={state.globalFilter}
setGlobalFilter={setGlobalFilter}
/>
</th>
</tr>
{headerGroups.map((headerGroup) => (
<tr
{...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map((column)
=> (
<th {...column.getHeaderProps()}>
{column.render("Header")}
{/* Rendering Default Column Filter
*/}
<div>
{column.canFilter ?
column.render("Filter")
:null}
</div>
</th>
))}
</tr>
))}
</thead>
<tbody {...getTableBodyProps()}>
{rows.map((row, i) => {
prepareRow(row);
return (
<tr {...row.getRowProps()}>
{row.cells.map((cell) => {
return <td {...cell.getCellProps()}>
{cell.render("Cell")}
</td>;
})}
</tr>
);
})}
</tbody>
</table>
);
}
Open App.js where we have defined the
array of columns.
Import SelectColumnFilter.
Add Filter: SelectColumnFilter to a
particular object of the column.
For using SelectColumnFilter,


{
Header: "Status",
accessor: "show.status",
Filter: SelectColumnFilter,
filter: "includes",
},
If you disable filter on any particular
column, add the following line of code-


{
Header: "Premiered",
accessor: "show.premiered",
// disable the filter for particular column
disableFilters: true,
Cell: ({ cell: { value } }) => value || "-",
},
How does
Column Filter
work in React
Table?
Do you remember we added a line for
implementing column filter-




<div>{column.canFilter ?
column.render("Filter") : null}</div>




The “Filter” is the property in the column
definition. It will render whichever
component is given as a value to the Filter
key.


Here we have used defaultcolumn, so no
need to define Filter for all the columns but
we have to define the Filter key for custom
filters (e.g. SelectColumnFilter)


The condition of column.canFilter will be
executed and it will render component
defined to key Filter.
Here we have mentioned the custom filter,
SelectColumnFilter, in the object and set
DefaultFilterForColumn as a default shown
filter.
Github
Repository:
react-table-
example
Feel free to visit the source code of the
demo application- react-table-example.
Conclusion
So, this was about how to filter your table
data using the useFilter hook. I hope the
React Table tutorial was helpful for you. If
you have any questions or suggestions, feel
free to comment below.


If you are a ReactJs enthusiast, check the
ReactJS Tutorials page with more tutorials
and its respective github repository.


At Bacancy, developers try their best to
come up with optimum solutions and
affordable problem-solving approaches. If
you are looking for a helping hand for your
project, contact us to hire ReactJS
developers without wasting a minute.
www.bacancytechnology.com
Thank You
Ad

Recommended

How to implement g rpc services in nodejs
How to implement g rpc services in nodejs
Katy Slemon
 
React lecture
React lecture
Christoffer Noring
 
Rxjs marble-testing
Rxjs marble-testing
Christoffer Noring
 
Graphql, REST and Apollo
Graphql, REST and Apollo
Christoffer Noring
 
Nativescript angular
Nativescript angular
Christoffer Noring
 
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...
탑크리에듀(구로디지털단지역3번출구 2분거리)
 
AJUG April 2011 Cascading example
AJUG April 2011 Cascading example
Christopher Curtin
 
Rxjs vienna
Rxjs vienna
Christoffer Noring
 
Java script – basic auroskills (2)
Java script – basic auroskills (2)
BoneyGawande
 
Pragmatic Functional Refactoring with Java 8
Pragmatic Functional Refactoring with Java 8
Codemotion
 
A Test of Strength
A Test of Strength
Chris Oldwood
 
Quick start with React | DreamLab Academy #2
Quick start with React | DreamLab Academy #2
DreamLab
 
Introduction to GraphQL
Introduction to GraphQL
Appier
 
Typescript barcelona
Typescript barcelona
Christoffer Noring
 
Firebase ng2 zurich
Firebase ng2 zurich
Christoffer Noring
 
Angular mix chrisnoring
Angular mix chrisnoring
Christoffer Noring
 
To-Do App With Flutter: Step By Step Guide
To-Do App With Flutter: Step By Step Guide
Biztech Consulting & Solutions
 
Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3
DreamLab
 
Rxjs swetugg
Rxjs swetugg
Christoffer Noring
 
Innovative Specifications for Better Performance Logging and Monitoring
Innovative Specifications for Better Performance Logging and Monitoring
Cary Millsap
 
Build Widgets
Build Widgets
scottw
 
Writing Good Tests
Writing Good Tests
Matteo Baglini
 
The Ring programming language version 1.9 book - Part 99 of 210
The Ring programming language version 1.9 book - Part 99 of 210
Mahmoud Samir Fayed
 
Test-driven Development with AEM
Test-driven Development with AEM
Jan Wloka
 
Converting Db Schema Into Uml Classes
Converting Db Schema Into Uml Classes
Kaniska Mandal
 
The Ring programming language version 1.5.1 book - Part 175 of 180
The Ring programming language version 1.5.1 book - Part 175 of 180
Mahmoud Samir Fayed
 
Unit Testing at Scale
Unit Testing at Scale
Jan Wloka
 
Matteo Collina | Take your HTTP server to Ludicrous Speed | Codmeotion Madrid...
Matteo Collina | Take your HTTP server to Ludicrous Speed | Codmeotion Madrid...
Codemotion
 
React table tutorial use filter (part 2)
React table tutorial use filter (part 2)
Katy Slemon
 
Advantage of Table in React JS.pdf
Advantage of Table in React JS.pdf
Nishaadequateinfosof
 

More Related Content

What's hot (20)

Java script – basic auroskills (2)
Java script – basic auroskills (2)
BoneyGawande
 
Pragmatic Functional Refactoring with Java 8
Pragmatic Functional Refactoring with Java 8
Codemotion
 
A Test of Strength
A Test of Strength
Chris Oldwood
 
Quick start with React | DreamLab Academy #2
Quick start with React | DreamLab Academy #2
DreamLab
 
Introduction to GraphQL
Introduction to GraphQL
Appier
 
Typescript barcelona
Typescript barcelona
Christoffer Noring
 
Firebase ng2 zurich
Firebase ng2 zurich
Christoffer Noring
 
Angular mix chrisnoring
Angular mix chrisnoring
Christoffer Noring
 
To-Do App With Flutter: Step By Step Guide
To-Do App With Flutter: Step By Step Guide
Biztech Consulting & Solutions
 
Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3
DreamLab
 
Rxjs swetugg
Rxjs swetugg
Christoffer Noring
 
Innovative Specifications for Better Performance Logging and Monitoring
Innovative Specifications for Better Performance Logging and Monitoring
Cary Millsap
 
Build Widgets
Build Widgets
scottw
 
Writing Good Tests
Writing Good Tests
Matteo Baglini
 
The Ring programming language version 1.9 book - Part 99 of 210
The Ring programming language version 1.9 book - Part 99 of 210
Mahmoud Samir Fayed
 
Test-driven Development with AEM
Test-driven Development with AEM
Jan Wloka
 
Converting Db Schema Into Uml Classes
Converting Db Schema Into Uml Classes
Kaniska Mandal
 
The Ring programming language version 1.5.1 book - Part 175 of 180
The Ring programming language version 1.5.1 book - Part 175 of 180
Mahmoud Samir Fayed
 
Unit Testing at Scale
Unit Testing at Scale
Jan Wloka
 
Matteo Collina | Take your HTTP server to Ludicrous Speed | Codmeotion Madrid...
Matteo Collina | Take your HTTP server to Ludicrous Speed | Codmeotion Madrid...
Codemotion
 
Java script – basic auroskills (2)
Java script – basic auroskills (2)
BoneyGawande
 
Pragmatic Functional Refactoring with Java 8
Pragmatic Functional Refactoring with Java 8
Codemotion
 
Quick start with React | DreamLab Academy #2
Quick start with React | DreamLab Academy #2
DreamLab
 
Introduction to GraphQL
Introduction to GraphQL
Appier
 
Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3
DreamLab
 
Innovative Specifications for Better Performance Logging and Monitoring
Innovative Specifications for Better Performance Logging and Monitoring
Cary Millsap
 
Build Widgets
Build Widgets
scottw
 
The Ring programming language version 1.9 book - Part 99 of 210
The Ring programming language version 1.9 book - Part 99 of 210
Mahmoud Samir Fayed
 
Test-driven Development with AEM
Test-driven Development with AEM
Jan Wloka
 
Converting Db Schema Into Uml Classes
Converting Db Schema Into Uml Classes
Kaniska Mandal
 
The Ring programming language version 1.5.1 book - Part 175 of 180
The Ring programming language version 1.5.1 book - Part 175 of 180
Mahmoud Samir Fayed
 
Unit Testing at Scale
Unit Testing at Scale
Jan Wloka
 
Matteo Collina | Take your HTTP server to Ludicrous Speed | Codmeotion Madrid...
Matteo Collina | Take your HTTP server to Ludicrous Speed | Codmeotion Madrid...
Codemotion
 

Similar to React table tutorial project setup, use table, and usefilter (20)

React table tutorial use filter (part 2)
React table tutorial use filter (part 2)
Katy Slemon
 
Advantage of Table in React JS.pdf
Advantage of Table in React JS.pdf
Nishaadequateinfosof
 
Advantages and Disadvantage of table in react js-technical chamber.pdf
Advantages and Disadvantage of table in react js-technical chamber.pdf
Nishaadequateinfosof
 
An Introduction to ReactJS
An Introduction to ReactJS
All Things Open
 
Dive into React Performance
Dive into React Performance
Ching Ting Wu
 
React JS & Functional Programming Principles
React JS & Functional Programming Principles
Andrii Lundiak
 
React performance
React performance
Derek Willian Stavis
 
Is React reactive?
Is React reactive?
Maurice De Beijer [MVP]
 
2- Components.pdf
2- Components.pdf
jibreelkhan2
 
Advanced React Component Patterns - ReactNext 2018
Advanced React Component Patterns - ReactNext 2018
Robert Herbst
 
Integrating React.js with PHP projects
Integrating React.js with PHP projects
Ignacio Martín
 
React js
React js
Oswald Campesato
 
React/Redux
React/Redux
Durgesh Vaishnav
 
Learn react.js with me!
Learn react.js with me!
Shivani Thakur Daxini
 
Why React's Awesome!
Why React's Awesome!
nomanalikk
 
ES6 patterns in the wild
ES6 patterns in the wild
Joe Morgan
 
React.js Basics - ConvergeSE 2015
React.js Basics - ConvergeSE 2015
Robert Pearce
 
React API and Hooksfjhfhjfjhfhjfjfjhfjhf
React API and Hooksfjhfhjfjhfhjfjfjhfjhf
DharnaAhuja1
 
React for Dummies
React for Dummies
Mitch Chen
 
Powering code reuse with context and render props
Powering code reuse with context and render props
Forbes Lindesay
 
React table tutorial use filter (part 2)
React table tutorial use filter (part 2)
Katy Slemon
 
Advantage of Table in React JS.pdf
Advantage of Table in React JS.pdf
Nishaadequateinfosof
 
Advantages and Disadvantage of table in react js-technical chamber.pdf
Advantages and Disadvantage of table in react js-technical chamber.pdf
Nishaadequateinfosof
 
An Introduction to ReactJS
An Introduction to ReactJS
All Things Open
 
Dive into React Performance
Dive into React Performance
Ching Ting Wu
 
React JS & Functional Programming Principles
React JS & Functional Programming Principles
Andrii Lundiak
 
Advanced React Component Patterns - ReactNext 2018
Advanced React Component Patterns - ReactNext 2018
Robert Herbst
 
Integrating React.js with PHP projects
Integrating React.js with PHP projects
Ignacio Martín
 
Why React's Awesome!
Why React's Awesome!
nomanalikk
 
ES6 patterns in the wild
ES6 patterns in the wild
Joe Morgan
 
React.js Basics - ConvergeSE 2015
React.js Basics - ConvergeSE 2015
Robert Pearce
 
React API and Hooksfjhfhjfjhfhjfjfjhfjhf
React API and Hooksfjhfhjfjhfhjfjfjhfjhf
DharnaAhuja1
 
React for Dummies
React for Dummies
Mitch Chen
 
Powering code reuse with context and render props
Powering code reuse with context and render props
Forbes Lindesay
 
Ad

More from Katy Slemon (20)

React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
Katy Slemon
 
Data Science Use Cases in Retail & Healthcare Industries.pdf
Data Science Use Cases in Retail & Healthcare Industries.pdf
Katy Slemon
 
How Much Does It Cost To Hire Golang Developer.pdf
How Much Does It Cost To Hire Golang Developer.pdf
Katy Slemon
 
What’s New in Flutter 3.pdf
What’s New in Flutter 3.pdf
Katy Slemon
 
Why Use Ruby On Rails.pdf
Why Use Ruby On Rails.pdf
Katy Slemon
 
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
Katy Slemon
 
How to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdf
Katy Slemon
 
How to Build Laravel Package Using Composer.pdf
How to Build Laravel Package Using Composer.pdf
Katy Slemon
 
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
Katy Slemon
 
How to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdf
Katy Slemon
 
IoT Based Battery Management System in Electric Vehicles.pdf
IoT Based Battery Management System in Electric Vehicles.pdf
Katy Slemon
 
Understanding Flexbox Layout in React Native.pdf
Understanding Flexbox Layout in React Native.pdf
Katy Slemon
 
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
Katy Slemon
 
New Features in iOS 15 and Swift 5.5.pdf
New Features in iOS 15 and Swift 5.5.pdf
Katy Slemon
 
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
Katy Slemon
 
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
Katy Slemon
 
Flutter Performance Tuning Best Practices From the Pros.pdf
Flutter Performance Tuning Best Practices From the Pros.pdf
Katy Slemon
 
Angular Universal How to Build Angular SEO Friendly App.pdf
Angular Universal How to Build Angular SEO Friendly App.pdf
Katy Slemon
 
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
Katy Slemon
 
Ruby On Rails Performance Tuning Guide.pdf
Ruby On Rails Performance Tuning Guide.pdf
Katy Slemon
 
React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
Katy Slemon
 
Data Science Use Cases in Retail & Healthcare Industries.pdf
Data Science Use Cases in Retail & Healthcare Industries.pdf
Katy Slemon
 
How Much Does It Cost To Hire Golang Developer.pdf
How Much Does It Cost To Hire Golang Developer.pdf
Katy Slemon
 
What’s New in Flutter 3.pdf
What’s New in Flutter 3.pdf
Katy Slemon
 
Why Use Ruby On Rails.pdf
Why Use Ruby On Rails.pdf
Katy Slemon
 
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
Katy Slemon
 
How to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdf
Katy Slemon
 
How to Build Laravel Package Using Composer.pdf
How to Build Laravel Package Using Composer.pdf
Katy Slemon
 
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
Katy Slemon
 
How to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdf
Katy Slemon
 
IoT Based Battery Management System in Electric Vehicles.pdf
IoT Based Battery Management System in Electric Vehicles.pdf
Katy Slemon
 
Understanding Flexbox Layout in React Native.pdf
Understanding Flexbox Layout in React Native.pdf
Katy Slemon
 
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
Katy Slemon
 
New Features in iOS 15 and Swift 5.5.pdf
New Features in iOS 15 and Swift 5.5.pdf
Katy Slemon
 
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
Katy Slemon
 
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
Katy Slemon
 
Flutter Performance Tuning Best Practices From the Pros.pdf
Flutter Performance Tuning Best Practices From the Pros.pdf
Katy Slemon
 
Angular Universal How to Build Angular SEO Friendly App.pdf
Angular Universal How to Build Angular SEO Friendly App.pdf
Katy Slemon
 
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
Katy Slemon
 
Ruby On Rails Performance Tuning Guide.pdf
Ruby On Rails Performance Tuning Guide.pdf
Katy Slemon
 
Ad

Recently uploaded (20)

PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
Mastering AI Workflows with FME by Mark Döring
Mastering AI Workflows with FME by Mark Döring
Safe Software
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
ScyllaDB
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
Mastering AI Workflows with FME by Mark Döring
Mastering AI Workflows with FME by Mark Döring
Safe Software
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
ScyllaDB
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 

React table tutorial project setup, use table, and usefilter

  • 1. React Table Tutorial: Project Setup, useTable, and useFilter www.bacancytechnology.com
  • 3. Table user interfaces are ubiquitous, mostly used, and organized UI preferred by users and developers. It makes the data look simple and easily accessible. Being a ReactJS developer, you might have heard of React Table V7; few might have implemented it too. If you are looking for a tutorial explaining React Table V7 with an Example, you have chosen the right blog. Today we are here with a react-table tutorial for you. I understand how challenging it could be when you are trying to learn something new, but I also know how interesting is that! Isn’t it? Refer to the next section that includes the points covered in this tutorial – React Table Tutorial – Project Setup, Installation, useTable, and useFilter.
  • 5. Before starting the development process let’s have a look at the below video so that you can understand what are we building. Video: https://youtu.be/5rXI5E9iCkE
  • 7. The creator of React Table – Tanner Linsley, launched React Table v7 in March 2020. We might remember using the class component of react-table, but now the library provides the Hooks-based APIs and plugins for creating a hassle-free React Table. The release is considered a significant change as the approach to creating a table, table UI, and style has changed.
  • 8. New Features in React Table v7 Headless (100% customizable, Bring- your-own-UI) Lightweight (5kb – 14kb+ depending on features used and tree-shaking) Sorting (Multi and Stable) Filters Animatable Row Expansion Column Ordering Virtualizable Server-side/controlled data/state Auto out of the box, fully controllable API Considering React Table release note, here are the new features in React Table v7:
  • 9. Extensible via a hook-based plugin system Row Selection Resizable Pivoting & Aggregation
  • 11. Create ReactJS project using the below command. npx create-react-app react-table-demo
  • 13. Install react-table and axios. npm install react-table axios --save //npm yarn add react-table axios //yarn
  • 14. Looking for proficient ReactJs developers for your project? Bacancy has the best developers with extraordinary problem-solving skills and Javascript knowledge. Get in touch with us today and hire ReactJS developer to build your dream product.
  • 16. App.js – main file TableContainer.js – having a Table component. After done with project setup and installation, follow these steps to implement React Table Example. I’ll be writing the entire code in two files, i.e.,
  • 17. Importing Axios and Hooks import React, { useState, useEffect, useMemo } from "react"; import axios from "axios"; Initializing state using useState const [data, setData] = useState([]);
  • 18. Fetching Data using Axios useEffect(() => { axios("http://api.tvmaze.com/search/shows ?q=girls") .then((res) => { setData(res.data); }) .catch((err) => console.log(err)) }, []);
  • 19. I have called “http://api.tvmaze.com/search/shows? q=girls“ If the promise is resolved, it will execute then block, in which we will store the response in the state using setData(res.data) And if the promise is rejected, it will execute the catch block and console the error.
  • 20. Defining Columns Header – the name of the column Accessor – key in data. After preparing our data, let’s define the columns of the Table. The column structure would consist- We will wrap it inside hook useMemo as far as the optimization is concerned. const columns = useMemo( () => [ { Header: "TV Show", columns: [ {
  • 21. Header: "Name", accessor: "show.name" }, { Header: "Type", accessor: "show.type" }, { Header: "Language", accessor: "show.language" }, { Header: "Official Site", accessor: "show.officialSite", Cell: ({ cell: { value } }) => value ? {value} : "-" }, { Header: "Rating", accessor: "show.rating.average", Cell: ({ cell: { value } }) => value || "-" },
  • 22. { Header: "Status", accessor: "show.status", }, { Header: "Premiered", accessor: "show.premiered", Cell: ({ cell: { value } }) => value || "-" }, { Header: "Time", accessor: "show.schedule.time", Cell: ({ cell: { value } }) => value || "-" }, ] } ] )
  • 23. You might be wondering why I have written “show.name”, “show.type”, “show.rating.average” and so on. It is because the data is inside the show object, and for accessing the data, we will use show. as the prefix. Here is the sample of data-
  • 24. Custom Cell { Header: " Official Site ", accessor: " show.officialSite ", Cell: ( props ) => { return < YourComponent { ...props } / > } }, We can have the custom cell for each row as shown above. A cell has access to the values of each row; you can console props to see what it consists of. Our demo will implement the custom cell to check whether show.officalSite has the value or not. If it has the value then it will return or “-”
  • 25. { Header: "Official Site", accessor: "show.officialSite", Cell: ({ cell: { value } }) => value ? {value} : "-" },
  • 27. Data consists of the data of the API response Columns is an array of objects for defining table columns. We will create another file named – TableContainer.js in which we will build our Table component using the useTable hook. It will take two properties: data and columns, which we have defined in the above sections.
  • 28. import React from "react"; import { useTable } from "react-table"; export default function Table({ columns, data }) { const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow, } = useTable({ columns, data, }) return ( < table {...getTableProps()} > < head > {headerGroups.map(headerGroup => ( < tr {...headerGroup.getHeaderGroupProps()} > {headerGroup.headers.map(column =
  • 29. > ( < th {...column.getHeaderProps()}> {column.render('Header')}< /th > ))} ))} < /thead > < tbody {...getTableBodyProps()} > {rows.map((row, i) => { prepareRow(row) return ( < tr {...row.getRowProps()} > {row.cells.map(cell => { return < td {...cell.getCellProps()}> {cell.render('Cell')}< /td > })} < /tr > ) })} < /tbody > < /table > ) }
  • 31. Import the Table from the TableContainer.js and then render on the UI using import Table from './TableContainer' < div className="App" > < h1 >< center >React Table Demo< /center >< /h1 > < Table columns={columns} data={data} /> < /div >
  • 32. After implementing the above code snippets, following your App.js and TableContainer.js will look like this – // App.js import React, { useState, useEffect, useMemo } from "react"; import axios from "axios"; import { useTable } from "react-table"; import './App.css'; function Table({ columns, data }) { const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow,
  • 33. } = useTable({ columns, data, }) return ( < table {...getTableProps()} > < head > {headerGroups.map(headerGroup => ( < tr {...headerGroup.getHeaderGroupProps()}> {headerGroup.headers.map(column => ( < th {...column.getHeaderProps()}> {column.render('Header')}< /th > ))} < /tr > ))} < /thead > < tbody {...getTableBodyProps()}> {rows.map((row, i) => {
  • 34. prepareRow(row) return ( < tr {...row.getRowProps()}> {row.cells.map(cell => { return {cell.render('Cell')} })} < /tr > ) })} < /tbody > < /table > ) } function App() { const [data, setData] = useState([]); useEffect(() => { axios("http://api.tvmaze.com/search/shows ?q=girls")
  • 35. .then((res) => { setData(res.data); }) .catch((err) => console.log(err)) }, []); const columns = useMemo( () => [ { Header: "TV Show", columns: [ { Header: "Name", accessor: "show.name" }, { Header: "Type", accessor: "show.type" }, { Header: "Language", accessor: "show.language" },
  • 36. { Header: "Official Site", accessor: "show.officialSite", Cell: ({ cell: { value } }) => value ? {value} : "-" }, { Header: "Rating", accessor: "show.rating.average", Cell: ({ cell: { value } }) => value || "-" }, { Header: "Status", accessor: "show.status", }, { Header: "Premiered", accessor: "show.premiered", Cell: ({ cell: { value } }) => value || "-" },
  • 37. { Header: "Time", accessor: "show.schedule.time", Cell: ({ cell: { value } }) => value || "-" }, ] } ] ) return ( < div className="App" > < h1 >< center >React Table Demo< /center >< /h1 > < Table columns={columns} data={data} / > < /div > ); } export default App;
  • 38. // TableContainer.js import React from "react"; import { useTable } from "react-table"; export default function Table({ columns, data }) { const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow, } = useTable({ columns, data, })
  • 39. return ( < table {...getTableProps()}> < head > {headerGroups.map(headerGroup => ( < tr {...headerGroup.getHeaderGroupProps()}> {headerGroup.headers.map(column => ( < th {...column.getHeaderProps()}> {column.render('Header')} ))} < /tr > ))} < /thead > < tbody {...getTableBodyProps()} > {rows.map((row, i) => { prepareRow(row) return ( < tr {...row.getRowProps()}> {row.cells.map(cell => { return {cell.render('Cell')} })}
  • 40. < /tr > ) })} < /tbody > < /table > ) } After running the command npm run start you will see something like this-
  • 42. Now, moving on to useFilter and useGlobalFilter in our application. According to our UI, this will be our project structure. Update App.js TableContainer.js We will implement the default filter and select column filter. For that, we will Create a new file named – Filter.js (which will have functional components for Filter views) Without further ado, let’s create Filter.js and define React table components for Filter UI.
  • 44. Default Filter for Columns: It will render text input, and the column data is filtered based on the text entered. Global Filter: It will render text input but not just for columns; the entire table data is filtered based on the text entered. Select Filter for Column: It will render select input, and the column data is filtered based on the option selected from the list. In this React Table demo, we will implement three filter views –
  • 45. We will create a common file named Filter.js (or with any suitable name) from where we will export the above-mentioned functional components for readability purposes. // Filter.js import { React, useMemo, useState } from "react"; import { useAsyncDebounce } from "react- table"; import { Label, Input } from "reactstrap"; // Component for Global Filter export function GlobalFilter({ globalFilter, setGlobalFilter }) { const [value, setValue] = useState(globalFilter);
  • 46. const onChange = useAsyncDebounce((value) => { setGlobalFilter(value || undefined); }, 200); return ( <div> <Label>Search Table: </Label> <Input value={value || ""} onChange={(e) => { setValue(e.target.value); onChange(e.target.value); }} placeholder=" Enter value " className="w-25" style={{ fontSize: "1.1rem", margin: "15px", display: "inline", }}
  • 47. /> </div> ); } // Component for Default Column Filter export function DefaultFilterForColumn({ column: { filterValue, preFilteredRows: { length }, setFilter, }, }) { return ( <Input value={filterValue || ""} onChange={(e) => { // Set undefined to remove the filter entirely setFilter(e.target.value || undefined); }}
  • 48. placeholder={`Search ${length} records..`} style={{ marginTop: "10px" }} /> ); } // Component for Custom Select Filter export function SelectColumnFilter({ column: { filterValue, setFilter, preFilteredRows, id }, }) { // Use preFilteredRows to calculate the options const options = useMemo(() => { const options = new Set(); preFilteredRows.forEach((row) => { options.add(row.values[id]); }); return [...options.values()]; }, [id, preFilteredRows]);
  • 49. // UI for Multi-Select box return ( <select value={filterValue} onChange={(e) => { setFilter(e.target.value || undefined); }} > <option value="">All</option> {options.map((option, i) => ( <option key={i} value={option}> {option} </option> ))} </select> ); }
  • 50. Explanation What is the use of useAsyncDebounce? – React table provides useAsyncDebounce to avoid the multiple re-renders caused due to side-effects and to use the latest one. Back-to-back state side-effects take place that triggers multiple renders. So, rather than handling it manually, React Table provides a hook to debounce the rapid side- effects. Here, we have little data, so that we won’t realize the importance of useAsyncDebounce. But, consider, if we have thousands of data filtered, then the continuous state changes and side-effects are much costlier than this demo app. A good developer takes care of the performance even while coding for a demo application. Try avoiding trash coding.
  • 51. The setfilter, filterValue, and preFilterRows are column properties used for specific columns. We have destructured the column prop and used them to get the filter values of respective columns.
  • 53. For GlobalFilter and DefaultFilterForColumn Open TableContainer.js and Import components and hooks- import { useTable, useFilters, useGlobalFilter } from "react-table"; import { GlobalFilter, DefaultFilterForColumn} from "./Filter";
  • 54. Pass DefaultFilterForColumn to useTable hook as a default filter for all the columns. So by default, your columns will have these components as filters unless you provide a custom filter or disable the filter. useTable( { columns, data, defaultColumn: { Filter: DefaultFilterForColumn }, }, useFilters, useGlobalFilter
  • 55. Now for rendering the UI, use the following code- {/* Rendering Global Filter */} {/* Rendering Default Column Filter */} {column.canFilter ? column.render("Filter") : null}
  • 56. Your entire TableContainer.js will look like this- // TableContainer.js import { React } from "react"; import { useTable, useFilters, useGlobalFilter } from "react-table"; import { GlobalFilter, DefaultColumnFilter } from "./Filter"; export default function Table({ columns, data }) { const { getTableProps, getTableBodyProps, headerGroups, rows, state, visibleColumns, prepareRow,
  • 57. setGlobalFilter, preGlobalFilteredRows, } = useTable( { columns, data, defaultColumn: { Filter: DefaultFilterForColumn }, }, useFilters, useGlobalFilter ); return ( <table {...getTableProps()}> <thead> <tr> <th colSpan={visibleColumns.length} style={{
  • 58. textAlign: "center", }} > {/* Rendering Global Filter */} <GlobalFilter preGlobalFilteredRows= {preGlobalFilteredRows} globalFilter={state.globalFilter} setGlobalFilter={setGlobalFilter} /> </th> </tr> {headerGroups.map((headerGroup) => ( <tr {...headerGroup.getHeaderGroupProps()}> {headerGroup.headers.map((column) => ( <th {...column.getHeaderProps()}> {column.render("Header")} {/* Rendering Default Column Filter */}
  • 59. <div> {column.canFilter ? column.render("Filter") :null} </div> </th> ))} </tr> ))} </thead> <tbody {...getTableBodyProps()}> {rows.map((row, i) => { prepareRow(row); return ( <tr {...row.getRowProps()}> {row.cells.map((cell) => { return <td {...cell.getCellProps()}> {cell.render("Cell")} </td>; })}
  • 60. </tr> ); })} </tbody> </table> ); } Open App.js where we have defined the array of columns. Import SelectColumnFilter. Add Filter: SelectColumnFilter to a particular object of the column. For using SelectColumnFilter, { Header: "Status", accessor: "show.status", Filter: SelectColumnFilter, filter: "includes", },
  • 61. If you disable filter on any particular column, add the following line of code- { Header: "Premiered", accessor: "show.premiered", // disable the filter for particular column disableFilters: true, Cell: ({ cell: { value } }) => value || "-", },
  • 62. How does Column Filter work in React Table?
  • 63. Do you remember we added a line for implementing column filter- <div>{column.canFilter ? column.render("Filter") : null}</div> The “Filter” is the property in the column definition. It will render whichever component is given as a value to the Filter key. Here we have used defaultcolumn, so no need to define Filter for all the columns but we have to define the Filter key for custom filters (e.g. SelectColumnFilter) The condition of column.canFilter will be executed and it will render component defined to key Filter.
  • 64. Here we have mentioned the custom filter, SelectColumnFilter, in the object and set DefaultFilterForColumn as a default shown filter.
  • 66. Feel free to visit the source code of the demo application- react-table-example.
  • 68. So, this was about how to filter your table data using the useFilter hook. I hope the React Table tutorial was helpful for you. If you have any questions or suggestions, feel free to comment below. If you are a ReactJs enthusiast, check the ReactJS Tutorials page with more tutorials and its respective github repository. At Bacancy, developers try their best to come up with optimum solutions and affordable problem-solving approaches. If you are looking for a helping hand for your project, contact us to hire ReactJS developers without wasting a minute.