score:0

Accepted answer

i don't know why you are using react-router-dom and then not really use it for what it was designed for. you are working with function components, so as far as i can tell, any solution will require a react hook. whether you just use the useparams hook to get the id to filter by, or if you declare an id state in the parent with usestate, or create a react context and use both usestate and usecontext, or use redux and usedispatch and useselector. do you see where this is headed?

i suggest just using the useparams hook as it's the most trivial to implement.

  1. fix the character bio route so the id route match param is easier to read and consume.

    <route path="/charbio/:id" element={<charbio />} />
    

    with path="/charbio:id" the link would inject a leading : character into the id with to={`/charbio${element.id}`}, i.e. instead of "goku" the id param would be ":goku", and this doesn't work easily for filtering.

  2. fix the link in perso so it's linking to a "/charbio/:id" path.

    <link to={`/charbio/${element.id}`}>
      <h1>{element.id}</h1>
    </link>
    
  3. use the useparams hook in the charbio component and filter the api data by id.

    export default function charbio() {
      const { id } = useparams();
      const element = api.find(el => el.id === id);
      return element ? <p>{element.bio}</p> : null;
    }
    

Related Query

More Query from same tag