Basically what I have is an array of projects (objects) with their names and technologies used (that's another array). What I am trying to do is to filter the projects based on which button it's clicked. For example if a user clicks the 'React' button it should only display the projects that have 'React' in their array of technologies. If a user clicks the 'All' button it should display all the projects. Here is the code that I have:
const projects = [
{
name: 'Poject 1',
technologies: ['React', 'Gatsby']
},
{
name: 'Project 2',
technologies: ['HTML', 'Node.js']
},
{
name: 'Project 3',
technologies: ['React', 'Node.js']
},
{
name: 'Project 4',
technologies: ['HTML', 'CSS']
}
]
const displayProjects = () => {
const [tech, setTech] = useState('')
const filteredProjects = projects.filter((project) => {
return project.technologies.some(t => t === tech)
})
return (
<button>All</button>
<button onClick={() => setTech(tech === 'React')}>React</button>
<button onClick={() => setTech(tech === 'HTML')}>HTML</button>
{filteredProjects.map(function (filteredProject) {
return (
<div>
{filteredProject.name}
</div>
)
})}
)
}
I realize that I missing something in my code but I feel like I am close to getting to the solution. Any suggestions are hugely appreciated.
