简体   繁体   English

在 map ZC1C425268E68385D1AB5074C17A4 内调用更新 state function

[英]Call update state function within a map function

I am looking to add an auto increment counter to a map function that would populate a question number on my component.我希望在 map function 中添加一个自动增量计数器,它会在我的组件上填充一个问题编号。

I am not sure how best to call this function within the map function I have in place and any help would be greatly appreciated.我不确定如何最好地在 map function 中调用此 function,我将不胜感激。 I would be looking to place the updated count in the Typography with "Open Question" output.我希望将更新后的计数放入带有“未决问题”output 的排版中。

    const [questionCounter, setQuestionCounter] = useState(0)
    const updateCounter = (e) => {
        setQuestionCounter(questionCounter++)
      }
    {quiz.question.map((questions) => (
          <Card elevation={6}>
            <Grid>
              <Accordion>
                <AccordionSummary
                  expandIcon={<ExpandMoreIcon />}
                >
                  <Typography>Open Question</Typography>

As suggested I am now using the index within the map function and adding 1 to the base when it starts so that each question has the correct number against it now in the accordion summary.正如建议的那样,我现在正在使用 map function 中的索引,并在开始时将基数加 1,以便每个问题现在在手风琴摘要中都有正确的数字。

 <Typography className={classes.quizDescription}>{quiz.longDescription}</Typography>
        {quiz.question.map((questions, index) => (
          <Card elevation={6}>
            <Grid>
              <Accordion>
                <AccordionSummary
                  expandIcon={<ExpandMoreIcon />}
                >
                  <Typography>Open Question {index + 1}</Typography>
                </AccordionSummary>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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