简体   繁体   中英

how to add href link with parameters fetched using api in react?

I have a frontend using react . I want to add a link after the map to the survey page using parameters extracted from data fetched from the API. I have two problems:

  • I don't know how to add the parameters to the link

  • The link could not be added after the function map it gives an error.

Here is my code:

    export default function Dashboard() {
    const [students, setstudents] = useState([]);
     useEffect(() => {
     const fetchStudents = async () => {
     try {
    const resp = await Axios({
      method: "GET",
      url: "http://localhost:3001//student/courses",
      headers: {
        "Content-Type": "application/json",
      },
    });
    setstudents(resp.data);
  } catch (err) {}
  };
  fetchStudents();
      }, []);
  const classes = useStyles();
  return (
  <GridContainer>
  <GridItem xs={12} sm={6} md={3}>
    <Card>
      <CardHeader color="warning" stats icon>
        <CardIcon color="warning">
          <Icon>content_copy</Icon>
        </CardIcon>
        <p className={classes.cardCategory}>Completed surveys</p>
        <h3 className={classes.cardTitle}>{students.toatl_voted}/{students.total_courses}</h3>
      </CardHeader>
    </Card>
  </GridItem>
  <GridItem xs={12} sm={12} md={12}>
    <Card>
      <CardHeader color="primary">
        <h4 className={classes.cardTitleWhite}>Available Surveys</h4>
        <p className={classes.cardCategoryWhite}>
          Here is the remaining courses surveys to submit
        </p>
      </CardHeader>
      <CardBody>
        <Table
          tableHeaderColor="primary"
          tableHead={["Course Code", "Course Name", "Survey Link"]}
          tableData={students && students.map((student) => [student.surveys.Course_code, 
          student.survey.Course_name])}


        //href link should be added here as follow api: url/student/course/:sectionId/:departmentId it must be clickeable


        />
      </CardBody>
    </Card>
  </GridItem>
   </GridContainer>
   );
     }

A lot of info seems to be missing from this question. But I will suggest the following as a starting place to solving this problem. Assumption 1: <Table /> 's tableData prop accepts the React.ReactNode proptype. Any React UI should be able to render a basic ReactNode. If not, opt for a different <Table /> API. The href template parameters in the following example are mock student properties.

...
const getTableData = students => {
    if(!Array.isArray(students) || !students.length){
        return null;
    }
    return students.map( student => [
        student.surveys.Course_code, 
        student.survey.Course_name,
        (<a href={`url/student/course/${student.sectionId}/${student.departmentId}`}>
             Department# {student.departmentId}
         </a>
        )
    ]);
};
<GridContainer>
    ...
    <Table
        tableData={getTableData(students)}
        tableHead={["Course Code", "Course Name", "Survey Link"]}
        tableHeaderColor="primary"
    />
    ...
</GridContainer>
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM