Generating a list of links from an object in React to create dynamic API routes

Viewed 32

I have an object that generates a list of id values which when clicked on should also be the id on an API call to retrieve the details. Just can't get it. It's not actually picking up the first part of the https:/sitename/ url. I am using hash router. I thought about creating a click handler and injecting the first part from a variable, but don't know if that is very efficient or elegant. What I have so far:

{Object.entries(this.state.searchData).map(([key, value]) => (
    <ul>
      <li key={key}>{key}:{' '}
        {Array.isArray(value) ? (
          <ul>
            {/* Refactor as a click handler to call api call for clicked link to return result */}
            {value.map(v => (<Link to={`_api/user/${v}`}><li>{v}</li></Link>))}
          </ul>) : String(value)}
      </li>
    </ul>
    )
  )
}
0 Answers
Related