'How to display WordPress Post LIST Rest API By Category? ReactJS

I am new to ReactJs and Need to implement wordpress Rest api, I am trying to figure out post list by category, My code is here.

 getPostsByCategory = async () => {
const ID = 103
try {
  const res = await axios.get(
    'https://xxxxxx.com/xxxxx/wp/v2/posts'
  )
  console.log('bd')
  console.log(Object.values(res).filter(pos => pos.categories.ID == 103))
  const result = Object.values(res.data.posts)
    .map(post => ({
      post,
      categories: post.categories.filter(c => c.ID.includes(ID)),
    }))
    .filter(c => c.length > 0)
  console.log(result)
  runInAction(() => {
    this.posts.replace(result)
  })
} catch (error) {
  // eslint-disable-line
  console.log(error)
} finally {
  this.setLoaded(true)
}

}

Please help me, It is very important.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source