Im trying to retrieve and filter JSON data with React.js and maybe hooks but I cant come with any good way to do it. Just now I have my JSON data local, so it does not come from restAPI or similar (for now)... and what Im trying is to create a menu with buttons that when I click on them I get filtered data. I know... it should not be very difficut to do it (cause Im doing it already in a veeeery basic way for a conditional that show different icones depending on if certain attribute is true.) but anyway im struggling with it.
Here is my code:
import React, {setState, useState} from 'react';
export const ByCourse = () => {
const projects = window.projects.aaData;
console.log('window.projects = ', projects);
return (
<div className="section-component">
<h2 className="mt-2 mb-4">By Course</h2>
<ul className="nav nav-tabs">
<li className="nav-item">
<a className="nav-link active" href="#">
All
</a>
</li>
<li className="nav-item">
<a className="nav-link" href="#">
Favorite
</a>
</li>
<li className="nav-item">
<a className="nav-link" href="#">
Recent
</a>
</li>
<li className="nav-item">
<a className="nav-link" href="#">
Assigned
</a>
</li>
</ul>
<div className="container">
{projects.map(project => {
return(
<div className="row">
<div className="w-100">
<div className="d-flex border mt-1 mb-1 p-3">
<div className="col-1">
{project.favorite ? <i className="fa fa-star"></i> : <i className="fa fa-star-o"></i>}
</div>
<div className="col-11">
<div className="font-weight-bold"> {project.projectTitle}</div>
<div className="font-weight-normal"> {project.ouName}</div>
</div>
</div>
</div>
</div>
)})}
</div>
</div>
);};
and my data looks like this:
projects = {"aaData":
[
{
"index":0,
"projectTitle":"123",
"userTitle":"VVS-teknik grund",
"ouName":"fertest1",
"orgUnitId":1022,
"projectId":2014,
"favorite":false,
"recent":false,
"userAssigned":false,
"projectStatus":"ACTIVE",
"viewLink":"http://xxxxxx
},
{
"index":1,
"projectTitle":"AAA",
"userTitle":"AAA",
"ouName":"fertest1",
"orgUnitId":1022,
"projectId":2002,
"favorite":false,
"recent":true,
"userAssigned":false,
"projectStatus":"ACTIVE",
"viewLink":"http://xxxxxx
},
{
"index":2,
"projectTitle":"Activity with image and text",
"userTitle":"asdas",
"ouName":"ferfer AB",
"orgUnitId":1004,
"projectId":1892,
"favorite":false,
"recent":false,
"userAssigned":true,
"projectStatus":"NEW",
"viewLink":"http://xxxxxx"
}]
I tried using a solution from here: Filtering JSON results in categories - React Hooks but I get a WEBPACK_1 ERROR and cant fix it...
Im also doing a very basic filtering here:
{project.favorite ? <i className="fa fa-star"></i> : <i className="fa fa-star-o"></i>}
and basically what Im looking for is:
if attribute "favorite" is true -> show results with attr favorite true else if attribute "recent" is true -> just show results with attribute recent == true Thank you very much in advance, any help will be very very welcome.
Here's a working example using
useStateto set a filter:https://codesandbox.io/s/crazy-turing-jd3ym?fontsize=14&hidenavigation=1&theme=dark