How to Use Data Tables with React JS Functional Components ? There are no any documentation
How to Use Data Tables with React JS Functional Components ? There are no any documentation
ankitpanchal15
Posts: 3Questions: 1Answers: 0
I'm Impress with Data Tables which is provide best Responsive and I want to use in React JS but i did not get any documentation.
Answers
The best is to search the web as there are plenty of resources out there, such as this Medium post,
Colin
I have checked that Medium Post, but it is not an updated post, it was Published in Jan 2017, and I want to use it with the latest version. Now React Updated and I want to use in Functional Component React JS.
I was able to get App.js converted to a functional component like this:
But my TblFunc.js file is still using Class Component like this:
I'm trying to use the useEffect and useRef hooks to convert TblFunc.js to a functional component but not having any luck. Hopefully, someone can figure it out and let us all know.
Actually, I think I may have figured it out. The only this is you need to hardcode your table ID that Datables will reference. I put it in a variable called "tableName". I have useRef in the code below but it doesn't work. Would be ideal to get useRef to work and reference the table instead of using id={tableName}
I was able to get this to work:
I followed this video which showed how to set up a Class Based Datatables component and then tried to convert everything to functional components. Still need to help with the referencing part so that we don't have to name our table IDs.
@mohsin106 It looks like you missed the link for the video - could you post that, please, as it may be useful for other users.
Colin
I'm not an expert by any means on React I'm afraid, but is
tableRef.current
the DOMtable
element? If so, you can use that ($(tableRef.current).DataTable(...)
) to construct the table.One very important point when using React is that you need it to not manipulate the table's DOM. You must let DataTables to that - since having two libraries trying to control the same DOM elements is only going to lead to disaster .
In Vue there is a
v-once
attribute for that - I presume there is something similar in React, but perhaps someone who knows more than me can say?Allan
Whoops, my bad. Here is the link for the video.
@allan , thanks for the tip...it seems to have worked. I must not have been referencing the useRef reference properly earlier when it was not working. But now, the below code appears to be working and I don't have to hardcode the tableName.